The String
class in Java provides a wide array of methods to manipulate and examine strings. These methods make it easier to perform common operations such as searching within strings, comparing strings, extracting substrings, and changing the case of characters within the string. Here’s an overview of some commonly used String
methods:
Length of a String
- length(): Returns the length (character count) of the string.java
String str = "Hello";
int len = str.length(); // 5
Substring
- substring(int beginIndex): Returns a new string that is a substring of this string, starting from
beginIndex
to the end of the original string. - substring(int beginIndex, int endIndex): Returns a new string that is a substring of this string, starting from
beginIndex
(inclusive) toendIndex
(exclusive).javaString str = "Hello, World!";
String substr1 = str.substring(7); // "World!"
String substr2 = str.substring(0, 5); // "Hello"
Comparing Strings
- equals(Object anotherObject): Compares this string to the specified object. The result is
true
if and only if the argument is notnull
and is aString
object that represents the same sequence of characters as this object. - equalsIgnoreCase(String anotherString): Compares this
String
to anotherString
, ignoring case considerations.javaString str1 = "Hello";
String str2 = "hello";
boolean eq = str1.equals(str2); // false boolean
eqIgnoreCase = str1.equalsIgnoreCase(str2); // true
Searching in Strings
- contains(CharSequence s): Returns
true
if and only if this string contains the specified sequence of char values. - indexOf(int ch): Returns the index within this string of the first occurrence of the specified character, or -1 if the character does not occur.
- lastIndexOf(int ch): Returns the index within this string of the last occurrence of the specified character, or -1 if the character does not occur.java
String str = "Hello, World!";
boolean contains = str.contains("World"); // true
int indexOfO = str.indexOf('o'); // 4
int lastIndexOfO = str.lastIndexOf('o'); // 8
Modifying Strings
- replace(char oldChar, char newChar): Returns a new string resulting from replacing all occurrences of
oldChar
in this string withnewChar
. - toLowerCase(): Converts all of the characters in this
String
to lower case using the rules of the default locale. - toUpperCase(): Converts all of the characters in this
String
to upper case using the rules of the default locale. - trim(): Returns a string whose value is this string, with any leading and trailing whitespace removed.java
String str = " Hello, World! ";
String replaced = str.replace(' ', '-'); // "-Hello,-World!-"
String lower = str.toLowerCase(); // " hello, world! "
String upper = str.toUpperCase(); // " HELLO, WORLD! "
String trimmed = str.trim(); // "Hello, World!"
Concatenation
- concat(String str): Concatenates the specified string to the end of this string.Alternatively, you can use thejava
String str1 = "Hello, ";
String str2 = "World!";
String concatenated = str1.concat(str2); // "Hello, World!"
+
operator for concatenation, which is more commonly used.
String Conversion and Other Methods
- valueOf(Various Types): This is a static method that returns the string representation of the passed argument. Overloaded versions exist for various data types.java
int num = 100;
String strNum = String.valueOf(num); // "100"
These are just a few of the many methods provided by the String
class in Java. Understanding and effectively using these methods can significantly enhance your ability to manipulate and process string data in your Java applications.