![]() |
VOOZH | about |
Java String methods are built-in functions provided by the String class to perform various operations on strings. These methods help in manipulating, comparing, searching, and formatting text efficiently. They make string handling easier and more convenient in Java programs.
Length: 13 Uppercase: GEEKSFORGEEKS Substring: eksf
Java provides a rich set of String methods that help perform various operations like comparison, searching, modification of string. Let's Understand one by one.
This method provides the total count of characters in the string.
13
This method returns the character at ith index.
W
This method return the substring from the ith index character to end.
World!
This method returns the substring from i to j-1 index.
World
This method appends the given string to the end of the current string.
Hello, World!!!!
This method returns the index within the string of the first occurrence of the specified string. If the specified string s is not found in the input string, the method returns -1 by default.
7
This method returns the index within the string of the first occurrence of the specified string, starting at the specified index.
10
This method returns the index within the string of the last occurrence of the specified string. If the specified string s is not found in the input string, the method returns -1 by default.
10
This method compares this string to the specified object.
true
This method checks if two strings are equal, without considering letter case.
true
This method compares two string lexicographically.
13
This method compares two string lexicographically, ignoring case considerations.
13
This method converts all the characters in the String to lower case.
hello, world!
This method converts all the characters in the String to upper case.
HELLO, WORLD!
This method returns the copy of the String, by removing whitespaces at both ends. It does not modify the whitespace characters present between the text.
'Hello, Trim!'
This method returns a new string where all instances of oldChar are replaced by newChar.
Hexxo, Worxd!
This method returns true if string contains the given string.
true
This method converts the string into a new character array.
H e l l o
This method returns true if string starts with this prefix.
true
String Methods | Description |
|---|---|
int length() | Returns the number of characters in the String. |
char charAt(int i) | Returns the character at ith index. |
String substring (int i) | Return the substring from the ith index character to end. |
String substring (int i, int j) | Returns the substring from i to j-1 index. |
String concat( String str) | Concatenates specified string to the end of this string. |
int indexOf (String s) | Finds the position of the first occurrence of the given substring within the main string. If the specified string s is not found in the input string, the method returns -1 by default. |
int indexOf (String s, int i) | Returns the index within the string of the first occurrence of the specified string, starting at the specified index. |
int lastIndexOf( String s) | Returns the index within the string of the last occurrence of the specified string. |
boolean equals( Object otherObj) | Compares this string to the specified object. |
boolean equalsIgnoreCase (String anotherString) | Compares string to another string, ignoring case considerations. |
int compareTo( String anotherString) | Compares two string lexicographically. |
int compareToIgnoreCase( String anotherString) | Compares two string lexicographically, ignoring case considerations. Note: In this case, it will not consider case of a letter (it will ignore whether it is uppercase or lowercase). |
String toLowerCase() | Converts all the characters in the String to lower case. |
String toUpperCase() | Converts all the characters in the String to upper case. |
String trim() | Returns the copy of the String, by removing whitespaces at both ends. Whitespace characters between words remain unchanged. |
String replace (char oldChar, char newChar) | Generates a new string where every instance of oldChar is substituted with newChar. Note: s1 is still feeksforfeeks and s2 is geeksgorgeeks |
boolean contains(CharSequence sequence) | Returns true if string contains the given string. |
Char[] toCharArray() | Converts this String to a new character array. |
boolean startsWith(String prefix) | Return true if string starts with this prefix. |