String Manipulation in Java

String Manipulation in Java

ยท

2 min read

String manipulation in Java is a common task when working with text data. Java provides a rich set of methods and libraries for working with strings, making it easy to perform operations such as concatenation, splitting, searching, replacing, and formatting. Here's an overview of string manipulation in Java:

1. Creating Strings:

You can create strings in Java using literals or by creating instances of the String class.

String str1 = "Hello, ";
String str2 = "World!";
String str3 = new String("Java");

2. Concatenation:

You can concatenate strings using the + operator or the concat() method.

String fullName = firstName + " " + lastName;
String greeting = str1.concat(str2);

3. Length:

You can find the length of a string using the length() method.

int length = str3.length();

4. Accessing Characters:

You can access individual characters in a string using the charAt() method.

char firstChar = str3.charAt(0); // Access the first character

5. Substrings:

You can extract substrings using the substring() method.

String part = str3.substring(1, 3); // Extract characters at indices 1 and 2

6. Searching and Replacing:

  • To check if a string contains a specific substring, you can use contains().

  • To find the index of a substring, use indexOf().

  • To replace substrings, use replace() or replaceAll().

boolean containsJava = str3.contains("Java");
int index = str3.indexOf("v");
String replaced = str3.replace("J", "S");

7. Splitting:

You can split a string into an array of substrings using split().

String sentence = "Java is a programming language";
String[] words = sentence.split(" ");

8. Formatting:

You can format strings using String.format() for complex formatting or printf() for simple formatting.

String formatted = String.format("Name: %s, Age: %d", name, age);
System.out.printf("Hello, %s%n", name);

9. Comparing Strings:

You can compare strings using equals(), equalsIgnoreCase(), or compareTo().

boolean isEqual = str1.equals(str2);
boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2);
int compareResult = str1.compareTo(str2);

10. String Builder and String Buffer:

For efficient string concatenation in a loop, consider using StringBuilder (not synchronized) or StringBuffer (synchronized) to avoid unnecessary object creation.

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Hello, ");
stringBuilder.append("World!");
String result = stringBuilder.toString();

String manipulation is a common task in Java programming, and understanding how to perform these operations efficiently can significantly improve your code's readability and performance.

Did you find this article valuable?

Support Karun's Blog by becoming a sponsor. Any amount is appreciated!