The String
class in Java is a fundamental class in the Java Standard Library. It is one of the most widely used classes in the Java programming language. It represents a sequence of characters and provides a number of methods for manipulating strings.
One of the most important characteristics of the String
class is that it is immutable, meaning that once a String
object is created, its value cannot be changed. This makes String
objects thread-safe, as they cannot be modified by multiple threads simultaneously.
Following are some examples of the methods available in the String
class:
length()
This method counts the number of characters in the string and returns the value.
Let’s take an example of the following code:
String str = "Hello, world!";
int length = str.length(); // length is 13
System.out.println(length);
The output of the above code will be:
13
Because in the variable str
there are 13 characters. Remember that the space is also a character.
charAt(int index)
This method returns the character at the specified index in the string.
Let’s take an example of the following code:
String str = "Hello, world!";
char c = str.charAt(1); // c is 'e'
System.out.println(c);
The output of the following code will be:
e
Because the starting index is zero (0). Hence, e is in the 1th position of the variable str
.
substring(int beginIndex, int endIndex)
This method returns a new string that is a substring of the original string. Starting at the specified beginIndex
and ending at the specified endIndex - 1
.
Let’s consider the following example:
String str = "Hello, world!";
String sub = str.substring(7, 12); // sub is "world"
System.out.println(sub);
The output of the above code will be:
world
indexOf(String str)
This method returns the index of the first occurrence of the specified string in the original string. For example:
String str = "Hello, world!";
int index = str.indexOf("world"); // index is 7
System.out.println(index);
The output of the above code will be:
7
Since this method returns the index of the first occurrence of the specified string, the following code also returns the same output:
String str = "Hello, world from the Coder Sathi World.";
int index = str.indexOf("world"); // index is 7
System.out.println(index);
The output of the above code is also the same. i.e. 7.
toLowerCase() and toUpperCase()
These methods return a new string with all the characters in the original string converted to lowercase or uppercase, respectively. See the example below:
String str = "Hello, world!";
String lower = str.toLowerCase(); // lower is "hello, world!"
String upper = str.toUpperCase(); // upper is "HELLO, WORLD!"
trim()
This method returns a new string with leading and trailing whitespace removed. See the example below:
String str = " Hello, world! ";
String trimmed = str.trim(); // trimmed is "Hello, world!"
split(String regex)
This method splits the string into an array of substrings, using the specified regular expression as the delimiter. See the example below:
String str = "Hello,world,how,are,you,today";
String[] words = str.split(","); // words is ["Hello", "world", "how", "are", "you", "today"]
There are many methods in the String class. You can refer to the official documentation to understand them in detail.