Case insensitive switch
Version ≥ Java SE 7
switch itself can not be parameterized to be case insensitive, but if absolutely required, can behave insensitively to the input string by using toLowerCase() or toUpperCase:
switch (myString.toLowerCase()) {
case “case1” :
…
break;
case “case2” :
…
break;
}
Beware
- Locale might affect how changing cases happen!
- Care must be taken not to have any uppercase characters in the labels – those will never get executed!
Replacing parts of Strings
Two ways to replace: by regex or by exact match.
Note: the original String object will be unchanged, the return value holds the changed String.
Exact match
Replace a single character with another single character:
String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of old Char in this string with newChar.
String s = "popcorn";
System.out.println(s.replace('p','W'));
Result:
WoWcorn
Replace sequence of characters with another sequence of characters:
String replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String s = "metal petal et al.";
System.out.println(s.replace("etal","etallica"));
Result:
metallica petallica et al.
Regex
Note: the grouping uses the $ character to reference the groups, like $1.
Replace all matches:
String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
String s = "spiral metal petal et al.";
System.out.println(s.replaceAll("(\w*etal)","$1lica"));
Result:
spiral metallica petallica et al.
Replace first match only:
String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement
String s = "spiral metal petal et al.";
System.out.println(s.replaceAll("(\w*etal)","$1lica"));
Result:
spiral metallica petal et al.
Getting the length of a String
In order to get the length of a String object, call the length() method on it. The length is equal to the number of UTF-16 code units (chars) in the string.
String str = "Hello, World!";
System.out.println(str.length()); // Prints out 13
A char in a String is UTF-16 value. Unicode codepoints whose values are ≥ 0x1000 (for example, most emojis) use two char positions. To count the number of Unicode codepoints in a String, regardless of whether each codepoint fits in a UTF-16 char value, you can use the codePointCount method:
int length = str.codePointCount(0, str.length());
You can also use a Stream of codepoints, as of Java 8:
int length = str.codePoints().count();
Getting the nth character in a String
String str = "My String";
System.out.println(str.charAt(0)); // "M"
System.out.println(str.charAt(1)); // "y"
System.out.println(str.charAt(2)); // " "
System.out.println(str.charAt(str.length-1)); // Last character "g"
To get the nth character in a string, simply call charAt(n) on a String, where n is the index of the character you would like to retrieve
NOTE: index n is starting at 0, so the first element is at n=0.
Counting occurrences of a substring or character in a string
countMatches method from org.apache.commons.lang3.StringUtils is typically used to count occurrences of a substring or character in a String:
import org.apache.commons.lang3.StringUtils;
String text = "One fish, two fish, red fish, blue fish";
// count occurrences of a substring
String stringTarget = "fish";
int stringOccurrences = StringUtils.countMatches(text, stringTarget); // 4
// count occurrences of a char
char charTarget = ',';
int charOccurrences = StringUtils.countMatches(text, charTarget); // 3
Otherwise for does the same with standard Java API’s you could use Regular Expressions:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String text = "One fish, two fish, red fish, blue fish";
System.out.println(countStringInString("fish", text)); // prints 4
System.out.println(countStringInString(",", text)); // prints 3
public static int countStringInString(String search, String text) {
Pattern pattern = Pattern.compile(search);
Matcher matcher = pattern.matcher(text);
int stringOccurrences = 0;
while (matcher.find()) {
stringOccurrences++;
}
return stringOccurrences;
}