Overview Of String Methods
Learn essential Java String methods: length(), charAt(), equals(), substring(), indexOf(), concat(), and case conversion. Master string manipulation techniques with practical examples for effective Java programming and text processing.
The String
class in Java provides various methods to manipulate and work with strings effectively. Below is an overview of some of the most commonly used methods, ranging from basic operations like checking the length of a string to more complex tasks like substring extraction and case conversion.
length()
The length()
method returns the number of characters in a string, including spaces and special characters.
Example:
String text = "Hello, World!";
int len = text.length(); // len will be 13
In this case, length()
returns 13 because the string contains 13 characters.
charAt()
The charAt()
method returns the character located at a specific index in the string. The index is zero-based, meaning the first character is at index 0.
Syntax:
char character = stringVariable.charAt(index);
Example:
String word = "Java";
char letter = word.charAt(2); // letter will be 'v'
In this example, charAt(2)
returns the character at index 2, which is 'v'.
isEmpty()
The isEmpty()
method checks whether a string is empty (i.e., has a length of 0). It returns true
if the string is empty and false
otherwise.
Example:
String emptyString = "";
boolean isEmpty = emptyString.isEmpty(); // isEmpty will be true
In this case, isEmpty()
returns true
because the string contains no characters.
equals()
The equals()
method is used to compare two strings for content equality. It returns true
if both strings contain the same sequence of characters and false
otherwise.
Theequals()
method should be used instead of==
for string content comparison. The==
operator checks whether the references to the strings are the same, not their content.
Example:
String str1 = "Hello";
String str2 = "Hello";
String str3 = "World";
boolean isEqual = str1.equals(str2); // true, content is the same
boolean notEqual = str1.equals(str3); // false, content differs
substring()
The substring()
method is used to extract a part of a string. It returns a new string that starts at the specified index and goes up to either the end of the string or another specified index.
Syntax:
String subString = stringVariable.substring(startIndex);
String subString = stringVariable.substring(startIndex, endIndex);
Example:
String text = "Hello, World!";
String sub1 = text.substring(7); // "World!"
String sub2 = text.substring(0, 5); // "Hello"
text.substring(7)
returns the substring starting at index 7 to the end.text.substring(0, 5)
returns the substring from index 0 to 5 (excluding the character at index 5).
concat()
The concat()
method is used to join two strings together. It appends the specified string to the end of the original string.
Syntax:
String newString = stringVariable.concat(anotherString);
Example:
String firstName = "John";
String lastName = "Doe";
String fullName = firstName.concat(" ").concat(lastName); // "John Doe"
Alternatively, string concatenation can also be done using the +
operator:
String fullName = firstName + " " + lastName; // "John Doe"
indexOf()
The indexOf()
method returns the index of the first occurrence of a specified character or substring in the string. If the character or substring is not found, it returns -1
.
Syntax:
int index = stringVariable.indexOf(characterOrSubstring);
Example:
String text = "Hello, World!";
int indexChar = text.indexOf('W'); // 7
int indexSub = text.indexOf("World"); // 7
int notFound = text.indexOf('x'); // -1
text.indexOf('W')
returns 7 because 'W' is located at index 7.text.indexOf("World")
returns 7 because "World" starts at index 7.text.indexOf('x')
returns-1
because 'x' is not in the string.
Case Conversion Methods
There are two commonly used methods to convert the case of strings: toLowerCase()
and toUpperCase()
. These methods return a new string with all characters converted to lowercase or uppercase, respectively.
toLowerCase()
This method converts all characters in the string to lowercase.
Example:
String text = "JAVA";
String lowerText = text.toLowerCase(); // "java"
toUpperCase()
This method converts all characters in the string to uppercase.
Example:
String text = "java";
String upperText = text.toUpperCase(); // "JAVA"
The methods provided by the String
class allow developers to efficiently perform a wide variety of operations on strings, from simple tasks like finding the length of a string or comparing two strings to more advanced tasks like substring extraction and case conversion. These methods are fundamental for manipulating and processing textual data in Java.