Java Tips -=-= Library =-=- Apache Commons Lang Part 6
- 2018年04月02日
- CATEGORY- 1. 技術力{技術情報}
前回に引き続き【Apache Commons Lang】をご紹介いたします。
今回は、
StringUtils
class
のサンプルをご紹介します。
Commons LangといえばStringUtilsと言われるくらい多用されるクラスです。
Apache Commons Langとは
Apache Commons Lang(アパッチ・コモンズ・ラング)は、ApacheのトッププロジェクトであるApache Commonsにある、Javaのjava.langパッケージを拡張するライブラリである。
– License:
Apache 2.0
– Home Page:
http://commons.apache.org/proper/commons-lang/
– Maven repository:
https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
– Maven dependency
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version> </dependency>
– Java doc:
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/package-summary.html
StringUtils
StringUtilsクラスは、文字列処理に関連する操作を提供します。
StringUtilsはnull入力文字列を静かに処理します。 つまり、null入力はnullを返します。 booleanまたはintが返される場合、詳細はメソッドによって異なります。
– public static boolean equals(CharSequence cs1, CharSequence cs2)
2つのキャラクターシーケンスを比較し同じ場合Trueを返す
/** * {@link StringUtils#equals(CharSequence, CharSequence) */ @Test public void testEquals() { assertThat(StringUtils.equals(null, null), is(false)); assertThat(StringUtils.equals(null, "abc"), is(false)); assertThat(StringUtils.equals("abc", null), is(false)); assertThat(StringUtils.equals("abc", "abc"), is(true)); assertThat(StringUtils.equals("abc", "ABC"), is(false)); }
– public static String getDigits(String str)
文字列に含まれる数字を連結して文字列として返す
/** * {@link StringUtils#getDigits(String) */ @Test public void testGetDigits() { assertThat(StringUtils.getDigits(null), nullValue()); assertThat(StringUtils.getDigits(""), is("")); assertThat(StringUtils.getDigits("abc"), is("")); assertThat(StringUtils.getDigits("1000$"), is("1000")); assertThat(StringUtils.getDigits("1123~45"), is("112345")); assertThat(StringUtils.getDigits("(541) 754-3010"), is("5417543010")); }
– public static String join(Object[] array, String separator)
文字配列を指定された区切りで結合し単一文字列を返す。
/** * {@link StringUtils#join(Object[], String) */ @Test public void testJoin() { assertThat(StringUtils.join(new String[] { null }, " "), is("")); assertThat(StringUtils.join(new String[] {}, ""), is("")); assertThat(StringUtils.join(new String[] { "a", "b", "c" }, "--"), is("a--b--c")); assertThat(StringUtils.join(new String[] { "a", "b", "c" }, null), is("abc")); assertThat(StringUtils.join(new String[] { "a", "b", "c" }, ""), is("abc")); assertThat(StringUtils.join(new String[] { null, "", "a" }, ','), is(",,a")); }
– public static String swapCase(String str)
文字列の小文字と大文字を入れ替える。
/** * {@link StringUtils#swapCase(String) */ @Test public void testSwapCase() { assertThat(StringUtils.swapCase(null), nullValue()); assertThat(StringUtils.swapCase(""), is("")); assertThat(StringUtils.swapCase("The dog has a BONE"), is("tHE DOG HAS A bone")); }
– public static String left(String str, int len)
文字列の左端から指定文字数だけ取得する。
/** * {@link StringUtils#left(String, int) */ @Test public void testLeft() { assertThat(StringUtils.left(null, 2), nullValue()); assertThat(StringUtils.left("ABC", -1), is("")); assertThat(StringUtils.left("", 2), is("")); assertThat(StringUtils.left("abc", 0), is("")); assertThat(StringUtils.left("abc", 2), is("ab")); assertThat(StringUtils.left("abc", 4), is("abc")); }
– public static String mid(String str, int pos, int len)
文字列の指定位置から指定位置まで文字列を抜き出す。
/** * {@link StringUtils#mid(String, int, int) */ @Test public void testMid() { assertThat(StringUtils.mid(null, 1, 5), nullValue()); assertThat(StringUtils.mid("ABC", 1, -1), is("")); assertThat(StringUtils.mid("", 0, 5), is("")); assertThat(StringUtils.mid("abc", 0, 2), is("ab")); assertThat(StringUtils.mid("abc", 0, 4), is("abc")); assertThat(StringUtils.mid("abc", 2, 4), is("c")); assertThat(StringUtils.mid("abc", 4, 2), is("")); assertThat(StringUtils.mid("abc", -2, 2), is("ab")); }
– public static String right(String str, int len)
文字列の右端から指定文字数だけ取得する。
/** * {@link StringUtils#right(String, int) */ @Test public void testRight() { assertThat(StringUtils.right(null, 2), nullValue()); assertThat(StringUtils.right("ABC", -1), is("")); assertThat(StringUtils.right("", 2), is("")); assertThat(StringUtils.right("abc", 0), is("")); assertThat(StringUtils.right("abc", 2), is("bc")); assertThat(StringUtils.right("abc", 4), is("abc")); }
– public static String repeat(String str, int repeat)
指定された文字列を指定回数繰り返した文字列を返す。
/** * {@link StringUtils#repeat(String, int) */ @Test public void testRepeat() { assertThat(StringUtils.repeat(null, 2) , nullValue()); assertThat(StringUtils.repeat("", 0) , is("")); assertThat(StringUtils.repeat("", 2) , is("")); assertThat(StringUtils.repeat("a", 3) , is("aaa")); assertThat(StringUtils.repeat("ab", 2) , is("abab")); assertThat(StringUtils.repeat("a", -2) , is("")); }
– public static int countMatches(CharSequence str, CharSequence str)
対象文字列内で指定文字列が出現した何回を返す。
/** * {@link StringUtils#countMatches(CharSequence, CharSequence) */ @Test public void testCountMatches() { assertThat(StringUtils.countMatches(null, "a"), is(0)); assertThat(StringUtils.countMatches("", "a"), is(0)); assertThat(StringUtils.countMatches("abba", null), is(0)); assertThat(StringUtils.countMatches("abba", ""), is(0)); assertThat(StringUtils.countMatches("abba", "a"), is(2)); assertThat(StringUtils.countMatches("abba", "ab"), is(1)); assertThat(StringUtils.countMatches("abba", "xxx"), is(0)); }
【Apache Commons Lang】いかがだったでしょうか。
次回も引き続き【Apache Commons Lang】をご紹介します。
【関連記事】
Google App Engine 第一回
Google App Engine 第二回
Javaのライブラリを手軽にテストしたい!! Groovy入門 第1回
Google App Engine 第三回
Google App Engine 第四回
Google App Engine 第五回
Google App Engine 第六回
Google App Engine 第七回
Google App Engine 第八回
Google App Engine 第九回
Google App Engine 第十回
Google App Engine 第十一回
Google App Engine 第十二回
AngularJS入門01
AngularJS入門02
AngularJS入門03
AngularJS入門04
AngularJS入門05
AngularJS入門06
AngularJS入門07
AngularJS入門08
AngularJS入門09
攻略 Elevator Saga =基本編=
攻略 Elevator Saga =応用編=
Java Tips -=-= Library =-=- JodaTime [前半]
Java Tips -=-= Library =-=- JodaTime [後半]
Java Tips -=-= Library =-=- Lombok Part 1
Java Tips -=-= Library =-=- Lombok Part 2
Java Tips -=-= Library =-=- Lombok Part 3
Java Tips -=-= Library =-=- Lombok Part 4
Java Tips -=-= Library =-=- Apache Commons Lang Part 1
Java Tips -=-= Library =-=- Apache Commons Lang Part 2
Java Tips -=-= Library =-=- Apache Commons Lang Part 3
Java Tips -=-= Library =-=- Apache Commons Lang Part 4
Java Tips -=-= Library =-=- Apache Commons Lang Part 5
- 2018年04月02日
- CATEGORY- 1. 技術力{技術情報}