Java String Manipulation
Common operations for working with strings: concatenation, splitting, searching, and formatting
java (1.0+)
2025-11-03
strings
text-processing
manipulation
Description
Strings in Java are immutable objects that represent sequences of characters. The String class provides numerous methods for manipulation, searching, and formatting. Understanding these methods is essential for effective Java programming.
Key Characteristics
- Immutability: Strings cannot be modified after creation
- String Pool: String literals are stored in a pool for reuse
- StringBuilder/StringBuffer: Mutable alternatives for frequent concatenation
Common Operations
- Concatenation: Combining strings
- Substring: Extracting parts of strings
- Search: Finding characters or substrings
- Replace: Replacing characters or substrings
- Split: Breaking strings into arrays
- Format: Creating formatted strings
- Case conversion: Converting between upper/lower case
Code
public class StringUtils { // Check if string is empty or null public static boolean isEmpty(String str) { return str == null || str.isEmpty(); } // Check if string is blank (whitespace only) public static boolean isBlank(String str) { return str == null || str.trim().isEmpty(); } // Reverse string public static String reverse(String str) { if (str == null) return null; return new StringBuilder(str).reverse().toString(); } // Count occurrences public static int countOccurrences(String str, String substring) { if (str == null || substring == null || substring.isEmpty()) { return 0; } int count = 0; int index = 0; while ((index = str.indexOf(substring, index)) != -1) { count++; index += substring.length(); } return count; } // Remove whitespace public static String removeWhitespace(String str) { if (str == null) return null; return str.replaceAll("\\s+", ""); } // Capitalize first letter public static String capitalize(String str) { if (isEmpty(str)) return str; return str.substring(0, 1).toUpperCase() + str.substring(1); } // Join strings public static String join(String delimiter, String... strings) { if (strings == null || strings.length == 0) return ""; StringBuilder result = new StringBuilder(strings[0]); for (int i = 1; i < strings.length; i++) { result.append(delimiter).append(strings[i]); } return result.toString(); } // Truncate string public static String truncate(String str, int maxLength) { if (str == null || str.length() <= maxLength) return str; return str.substring(0, maxLength) + "..."; }}
// String creationString str1 = "Hello";String str2 = new String("World");// ConcatenationString combined = str1 + " " + str2; // "Hello World"String withConcat = str1.concat(" Java"); // "Hello Java"// Lengthint length = str1.length(); // 5// Character accesschar first = str1.charAt(0); // 'H'// SubstringString sub1 = str1.substring(1); // "ello"String sub2 = str1.substring(1, 4); // "ell"// Searchboolean contains = str1.contains("ell"); // trueint index = str1.indexOf('l'); // 2int lastIndex = str1.lastIndexOf('l'); // 3boolean startsWith = str1.startsWith("He"); // trueboolean endsWith = str1.endsWith("lo"); // true// ReplaceString replaced = str1.replace('l', 'L'); // "HeLLo"String replacedAll = str1.replaceAll("l", "L"); // "HeLLo"String replacedFirst = str1.replaceFirst("l", "L"); // "HeLlo"// Case conversionString upper = str1.toUpperCase(); // "HELLO"String lower = upper.toLowerCase(); // "hello"// SplitString text = "one,two,three";String[] parts = text.split(","); // ["one", "two", "three"]// TrimString withSpaces = " Hello ";String trimmed = withSpaces.trim(); // "Hello"// FormatString formatted = String.format("Name: %s, Age: %d", "Alice", 30);// "Name: Alice, Age: 30"// StringBuilder for efficient concatenationStringBuilder sb = new StringBuilder();sb.append("Hello");sb.append(" ");sb.append("World");String result = sb.toString(); // "Hello World"// String comparisonboolean equals = str1.equals("Hello"); // trueboolean equalsIgnoreCase = str1.equalsIgnoreCase("HELLO"); // trueint compare = str1.compareTo("World"); // negative value// Check emptyboolean isEmpty = str1.isEmpty(); // falseboolean isBlank = " ".trim().isEmpty(); // true
Comments
No comments yet. Be the first to comment!
Please login to leave a comment.