String Methods
Here we will see some of the popular methods we can use with strings.
length(): This method is used to find the length of a string.
Example:
public class StringExample {
public static void main(String[] args) {
String quote = "To be or not to be";
System.out.println(quote.length());
}
}Output:
18
indexOf(): This method returns the first occurrence of a specified character or text in a string.
Example:
public class StringExample {
public static void main(String[] args) {
String quote = "To be or not to be";
System.out.println(quote.indexOf("be")); // index of text
System.out.println(quote.indexOf("r")); // index of character
}
}Output:
3
7
toLowerCase(): Converts string to lower case characters.
Example:
public class StringExample {
public static void main(String[] args) {
String quote = "THOR: Love and Thunder";
System.out.println(quote.toLowerCase());
}
}Output:
thor: love and thunder
toUpperCase(): Converts string to upper case characters.
Example:
public class StringExample {
public static void main(String[] args) {
String quote = "THOR: Love and Thunder";
System.out.println(quote.toUpperCase());
}
}Output:
THOR: LOVE AND THUNDER