String in Java
String is a sequence of characters enclosed in double quotes and are used to represent text in Java.
Here's an example of declaring and initializing String variables in Java:
String firstName = "Jake"; String lastName = "Xyz"; int age = 26;
In the above example, we have three variables: firstName, lastName, and age. These variables are declared as String data types. The firstName variable is assigned the value "Jake", indicating that it represents the first name of a person. The lastName variable is assigned the value "Xyz", representing the last name of that same person. Lastly, the age variable is assigned the value 26, which represents the age of the person.
Example:
public class Sample { public static void main(String[] args) { String firstName = "Jake"; String lastName = "Xyz"; int age = 26; System.out.println(firstName); System.out.println(lastName); System.out.println(age); } }
The output of the above code is as follows:
Jake Xyz 26
String Class
Java also provides String class to work with strings. The String class is immutable, which means once it is created, it cannot be changed. A String object can be created by using the new keyword with a constructor.
To create String object from String class, do the following:
String myStringObj = new String("Hello Universe");
String class Methods
The String class provides a wide range of methods for manipulating and working with strings. The following are some of the commonly used String methods in Java:
String Method | Description |
int length() | Returns the length of the string. |
char charAt(int index) | Returns the character at the specified index. |
String concat(String str) | Concatenates the specified string to the end of the current string. |
String toUpperCase() | Converts the string to uppercase. |
String toLowerCase() | Converts the string to lowercase. |
String substring(int beginIndex) | Returns a new string that is a substring of the original string, starting from the specified index. |
String substring(int beginIndex, int endIndex) | Returns a new string that is a substring of the original string, starting from the specified begin index and ending at the specified end index. |
int indexOf(String str) | Returns the index of the first occurrence of the specified substring within the string. |
int lastIndexOf(String str) | Returns the index of the last occurrence of the specified substring within the string. |
boolean startsWith(String prefix) | Checks if the string starts with the specified prefix. |
boolean endsWith(String suffix) | Checks if the string ends with the specified suffix. |
String replace(char oldChar, char newChar) | Replaces all occurrences of the specified old character with the new character. |
String replaceAll(String regex, String replacement) | Replaces all occurrences of the specified regular expression with the replacement string. |
String trim() | Removes leading and trailing whitespace from the string. |
String [] split(String regex) | Splits the string into an array of substrings based on the specified regular expression. |
Here is an example code that shows how to use the String class methods:
import java.util.Arrays; public class Sample { public static void main(String[] args) { String myText = "Hello Universe"; int textLength = myText.length(); System.out.println(textLength); char charAt = myText.charAt(0); System.out.println(charAt); String myText2 = myText.concat(". It's a beautiful day."); System.out.println(myText2); String upperCaseText = myText.toUpperCase(); System.out.println(upperCaseText); String lowerCaseText = myText.toLowerCase(); System.out.println(lowerCaseText); String subText = myText.substring(0, 5); System.out.println(subText); int indexOfCh = myText.indexOf("U"); System.out.println(indexOfCh); int lastIndexOfCh = myText.lastIndexOf("l"); System.out.println(lastIndexOfCh); boolean isTextStartingWith = myText.startsWith("He"); System.out.println(isTextStartingWith); boolean isTextEndingWith = myText.startsWith("rso"); System.out.println(isTextEndingWith); String replacedText = myText.replace("Universe", "World"); System.out.println(replacedText); String replacedAllText = myText.replaceAll("e", "E"); System.out.println(replacedAllText); String trimmedText = myText.trim(); System.out.println(trimmedText); String [] splittedTextArray = myText.split("\\s"); System.out.println(Arrays.toString(splittedTextArray)); } }
The output of the above code is as follows:
14 H Hello Universe. It's a beautiful day. HELLO UNIVERSE hello universe Hello 6 3 true false Hello World HEllo UnivErsE Hello Universe [Hello, Universe]