String Handling in Java
String handling in Java is an essential part of programming in Java. The String
class in Java provides a range of methods for manipulating strings, such as concatenation, comparison, searching, and more. Here are some key concepts and examples to illustrate string handling in Java:
Creating Strings in Java
Strings in Java can be created in several ways:
// Using string literal
String str1 = "Hello";
// Using new keyword
String str2 = new String("Hello");
// Using character array
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str3 = new String(charArray);
String Concatenation
Strings can be concatenated using the +
operator or the concat()
method:
String str1 = "Hello";
String str2 = "World";
// Using + operator
String result1 = str1 + " " + str2; // "Hello World"
// Using concat() method
String result2 = str1.concat(" ").concat(str2); // "Hello World"
String Length
To get the length of a string, use the length()
method:
String str = "Hello";
int length = str.length(); // 5
Character Extraction
Characters in a string can be accessed using the charAt()
method:
String str = "Hello";
char ch = str.charAt(1); // 'e'
Substring
Extracting a substring from a string can be done using the substring()
method:
String str = "Hello World";
String sub1 = str.substring(6); // "World"
String sub2 = str.substring(0, 5); // "Hello"
String Comparison
Strings can be compared using equals()
, equalsIgnoreCase()
, and compareTo()
methods:
String str1 = "Hello";
String str2 = "hello";
String str3 = "Hello";
// Using equals()
boolean isEqual = str1.equals(str3); // true
// Using equalsIgnoreCase()
boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2); // true
// Using compareTo()
int comparison = str1.compareTo(str2); // Negative value because "Hello" is less than "hello"
String Search
You can search for substrings or characters using indexOf()
and lastIndexOf()
methods:
String str = "Hello World";
// Using indexOf()
int index1 = str.indexOf('o'); // 4
int index2 = str.indexOf("World"); // 6
// Using lastIndexOf()
int lastIndex = str.lastIndexOf('o'); // 7
String Replace
Replacing characters or substrings in a string can be done using the replace()
method:
String str = "Hello World";
String replacedStr1 = str.replace('o', 'a'); // "Hella Warld"
String replacedStr2 = str.replace("World", "Java"); // "Hello Java"
String Split
Splitting a string into an array of substrings can be done using the split()
method:
String str = "Hello World Java";
String[] words = str.split(" "); // ["Hello", "World", "Java"]
String Trim
Trimming whitespace from the beginning and end of a string can be done using the trim()
method:
String str = " Hello World ";
String trimmedStr = str.trim(); // "Hello World"
Code Example: String Handling in Java
Here is a complete example program demonstrating some of the above methods:
public class StringHandlingExample {
public static void main(String[] args) {
String str = " Hello World ";
// Trim the string
String trimmedStr = str.trim();
System.out.println("Trimmed String: " + trimmedStr);
// Get the length of the trimmed string
int length = trimmedStr.length();
System.out.println("Length: " + length);
// Get a substring
String substring = trimmedStr.substring(6);
System.out.println("Substring: " + substring);
// Replace a character
String replacedStr = trimmedStr.replace('o', 'a');
System.out.println("Replaced String: " + replacedStr);
// Split the string
String[] words = trimmedStr.split(" ");
System.out.println("Split words:");
for (String word : words) {
System.out.println(word);
}
}
}
Conclusion
String handling in Java is powerful and versatile, thanks to the numerous methods provided by the String
class. Understanding and utilizing these methods can greatly enhance your ability to manipulate and manage string data effectively.