Apache Commons LangのStringUtilsについて

Apache Commons LangのStringUtilsは、文字列操作に便利なメソッドを提供するユーティリティクラスです。
以下にいくつかの一般的なメソッドとそれらの使用方法を示します。

String str = "Hello, world!";
if (StringUtils.isEmpty(str)) {
    System.out.println("文字列はnullまたは空です。");
} else {
    System.out.println("文字列はnullまたは空ではありません。");
}
String str = "  Hello, world!  ";
String trimmedStr = StringUtils.trim(str);
System.out.println(trimmedStr); 
// "Hello, world!"
String[] array = {"Hello", "world"};
String joinedStr = StringUtils.join(array, ", ");
System.out.println(joinedStr);
// "Hello, world"
String str = "Hello, world!";
String replacedStr = StringUtils.replace(str, "world", "Java");
System.out.println(replacedStr); 
// "Hello, Java!"
String str = "Hello, world!";
String reversedStr = StringUtils.reverse(str);
System.out.println(reversedStr); 
// "!dlrow ,olleH"
String str = "Hello, world!";
String substring = StringUtils.substring(str, 0, 5);
System.out.println(substring); 
// "Hello"
String str = "Hello, world!";
boolean startsWith = StringUtils.startsWith(str, "Hello");
boolean endsWith = StringUtils.endsWith(str, "world!");
System.out.println(startsWith);
// true
System.out.println(endsWith);
// true
String str1 = "hello";
String str2 = "HELLO";
boolean equalsIgnoreCase = StringUtils.equalsIgnoreCase(str1, str2);
System.out.println(equalsIgnoreCase); 
// true
String str = "Hello, world!";
String substring = StringUtils.substring(str, 7);
System.out.println(substring); 
// "world!"
String str = "Hello";
String paddedStr = StringUtils.rightPad(str, 10, '-');
System.out.println(paddedStr);
// "Hello-----"
String str = "Apple,Orange,Banana";
String[] splitArray = StringUtils.split(str, ",");
for (String element : splitArray) {
    System.out.println(element);
}
// 出力:
// "Apple"
// "Orange"
// "Banana"
String str = "aabbbccccdd";
String removedDuplicates = StringUtils.removeDuplicates(str);
System.out.println(removedDuplicates);
// "abcd"
String str = "##Hello, world!##";
String removedStart = StringUtils.removeStart(str, "##");
String removedEnd = StringUtils.removeEnd(removedStart, "##");
System.out.println(removedEnd); 
// "Hello, world!"
String str = "Hello, world!";
String replacedChars = StringUtils.replaceChars(str, "o", "0");
System.out.println(replacedChars); 
// "Hell0, w0rld!"
String str = "hello, world!";
String capitalized = StringUtils.capitalize(str);
String uncapitalized = StringUtils.uncapitalize(capitalized);
System.out.println(capitalized); 
// "Hello, world!"
System.out.println(uncapitalized); 
// "hello, world!"
String str = "Hello!";
String inserted = StringUtils.insert(str, 5, " world");
System.out.println(inserted); 
// "Hello world!"
String str = "12345";
boolean isNumeric = StringUtils.isNumeric(str);
System.out.println(isNumeric); 
// true
String str = "1234567890";
String maskedStr = StringUtils.overlay(str, "********", 3, 7);
System.out.println(maskedStr); 
// "123********90"

ほかにもまだまだありますが、何かの参考になればと思います。

参考文献

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です