String Methods

String Class

The String class in Java provides several methods that allow you to manipulate and work with strings. Here are some of the most commonly used methods:

String Methods In Detail


charAt(int index) 

This method returns the character at the specified index in the string. The index starts from 0 for the first character in the string and goes up to length()-1 for the last character. If the index is out of range, the method throws an IndexOutOfBoundsException. Here's an example:

String str = "Hello, World!";
char c = str.charAt(1);
System.out.println(c); // prints 'e'

length() 

This method returns the length of the string, which is the number of characters in the string. Here's an example:

String str = "Hello, World!";
int len = str.length();
System.out.println(len); // prints 13

substring(int beginIndex)

 This method returns a new string that is a substring of the original string, starting from the specified index to the end of the string. The index starts from 0 for the first character in the string, and goes up to length()-1 for the last character. Here's an example:

String str = "Hello, World!";
String substr = str.substring(7);
System.out.println(substr); // prints "World!"

substring(int beginIndex, int endIndex) 

This method 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 (exclusive). The index starts from 0 for the first character in the string and goes up to length()-1 for the last character. Here's an example:

String str = "Hello, World!";
String substr = str.substring(0, 5);
System.out.println(substr); // prints "Hello"

concat(String str)

 This method concatenates the specified string to the end of the original string and returns a new string. Here's an example:

String str = "Hello, ";
String str2 = "World!";
String result = str.concat(str2);
System.out.println(result); // prints "Hello, World!"

toUpperCase() 

This method returns a new string that is the uppercase version of the original string. Here's an example:

String str = "hello, world!";
String result = str.toUpperCase();
System.out.println(result); // prints "HELLO, WORLD!"

toLowerCase() 

This method returns a new string that is the lowercase version of the original string. Here's an example:

String str = "HELLO, WORLD!";
String result = str.toLowerCase();
System.out.println(result); // prints "hello, world!"

trim() 

This method returns a new string with leading and trailing white space removed. Here's an example:

String str = "    hello, world!    ";
String result = str.trim();
System.out.println(result); // prints "hello, world!"

equals(Object obj) 

This method compares the original string to the specified object and returns true if they are equal. Here's an example:

String str1 = "Hello, World!";
String str2 = "Hello, World!";
boolean result = str1.equals(str2);
System.out.println(result); // prints true

equalsIgnoreCase(String str)

This method compares the original string to the specified string ignoring the case and returns true if they are equal. Here's an example:

String str1 = "Hello, World!";
String str2 = "hello, world!";
boolean result = str1.equalsIgnoreCase(str2);
System.out

In conclusion, the String class in Java provides a range of methods for working with strings, including accessing individual characters, getting the length of a string, extracting substrings, concatenating strings, converting strings to uppercase or lowercase, removing whitespace, and comparing strings for equality. These methods can be very useful when working with text data in Java.