KEIS BLOGは株式会社ケイズ・ソフトウェアが運営しています。

KEIS BLOG

Java Tips -=-= Library =-=- Apache Commons Lang Part 5


前回に引き続き【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 isEmpty(CharSequence cs)

空白文字(null含む)のときtrueを返すします。

	/**
	 * {@link StringUtils#isEmpty(CharSequence)}
	 */
	@Test
	public void testIsEmpty() {
		assertThat(StringUtils.isEmpty(null), is(true));
		assertThat(StringUtils.isEmpty(""), is(true));
		assertThat(StringUtils.isEmpty(" "), is(false));
		assertThat(StringUtils.isEmpty("bbb"), is(false));
		assertThat(StringUtils.isEmpty("  bbb  "), is(false));
	}

– public static boolean isNotEmpty(CharSequence cs)

空白文字(null含む)ではないときtrueを返します。

	/**
	 * {@link StringUtils#isNotEmpty(CharSequence)}
	 */
	@Test
	public void testIsNotEmpty() {
		assertThat(StringUtils.isNotEmpty(null), is(false));
		assertThat(StringUtils.isNotEmpty(""), is(false));
		assertThat(StringUtils.isNotEmpty(" "), is(true));
		assertThat(StringUtils.isNotEmpty("bbb"), is(true));
		assertThat(StringUtils.isNotEmpty("  bbb  "), is(true));
	}

– public static boolean isBlank(CharSequence cs)

空白文字(null、スペース含む)のときtrueを返します。

	/**
	 * {@link StringUtils#isBlank(CharSequence)}
	 */
	@Test
	public void testIsBlank() {
		assertThat(StringUtils.isBlank(null), is(true));
		assertThat(StringUtils.isBlank(""), is(true));
		assertThat(StringUtils.isBlank(" "), is(true));
		assertThat(StringUtils.isBlank("bbb"), is(false));
		assertThat(StringUtils.isBlank("  bbb  "), is(false));
	}

– public static boolean isNotBlank(CharSequence cs)

空白文字(null、スペース含む)ではないときtrueを返します。

	/**
	 * {@link StringUtils#isNotBlank(CharSequence)}
	 */
	@Test
	public void testIsNotBlank() {
		assertThat(StringUtils.isNotBlank(null), is(false));
		assertThat(StringUtils.isNotBlank(""), is(false));
		assertThat(StringUtils.isNotBlank(" "), is(false));
		assertThat(StringUtils.isNotBlank("bbb"), is(true));
		assertThat(StringUtils.isNotBlank("  bbb  "), is(true));
	}

– public static boolean isAlpha(CharSequence cs)
アルファベットのときtrueを返します。

	/**
	 * {@link StringUtils#isAlpha(CharSequence)}
	 */
	@Test
	public void testIsAlpha() {
		assertThat(StringUtils.isAlpha(null), is(false));
		assertThat(StringUtils.isAlpha(""), is(false));
		assertThat(StringUtils.isAlpha("  "), is(false));
		assertThat(StringUtils.isAlpha("abc"), is(true));
		assertThat(StringUtils.isAlpha("ab2c"), is(false));
		assertThat(StringUtils.isAlpha("ab-c"), is(false));
	}

– public static boolean isAlphanumeric(CharSequence cs)

アルファベットまたは数値のときtrueを返します。

	/**
	 * {@link StringUtils#isAlphanumeric(CharSequence)}
	 */
	@Test
	public void testIsAlphanumeric() {
		assertThat(StringUtils.isAlphanumeric(null), is(false));
		assertThat(StringUtils.isAlphanumeric(""), is(false));
		assertThat(StringUtils.isAlphanumeric("  "), is(false));
		assertThat(StringUtils.isAlphanumeric("abc"), is(true));
		assertThat(StringUtils.isAlphanumeric("ab c"), is(false));
		assertThat(StringUtils.isAlphanumeric("ab2c"), is(true));
		assertThat(StringUtils.isAlphanumeric("ab-c"), is(false));
	}

– public static boolean isNumeric(CharSequence cs)

数値のときtrueを返します。

	/**
	 * {@link StringUtils#isNumeric(CharSequence)}
	 */
	@Test
	public void testIsNumeric() {
		assertThat(StringUtils.isNumeric(null), is(false));
		assertThat(StringUtils.isNumeric(""), is(false));
		assertThat(StringUtils.isNumeric("  "), is(false));
		assertThat(StringUtils.isNumeric("123"), is(true));
		assertThat(StringUtils.isNumeric("१२३"), is(true));
		assertThat(StringUtils.isNumeric("12 3"), is(false));
		assertThat(StringUtils.isNumeric("ab2c"), is(false));
		assertThat(StringUtils.isNumeric("12-3"), is(false));
		assertThat(StringUtils.isNumeric("12.3"), is(false));
		assertThat(StringUtils.isNumeric("-123"), is(false));
		assertThat(StringUtils.isNumeric("+123"), is(false));
	}

– public static boolean startsWith(CharSequence str, CharSequence prefix)
文字列の先頭が指定文字列で始まっていればtrueを返します。

	/**
	 * {@link StringUtils#startsWith(CharSequence, CharSequence)
	 */
	@Test
	public void testStartWith() {
		assertThat(StringUtils.startsWith(null, null), is(true));
		assertThat(StringUtils.startsWith(null, "abc"), is(false));
		assertThat(StringUtils.startsWith("abcdef", null), is(false));
		assertThat(StringUtils.startsWith("abcdef", "abc"), is(true));
		assertThat(StringUtils.startsWith("ABCDEF", "abc"), is(false));
	}

– public static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix)

文字列の先頭が指定文字列で始まっていればtrueを返す(大文字、小文字の区別なし)

	/**
	 * {@link StringUtils#startsWithIgnoreCase(CharSequence, CharSequence)
	 */
	@Test
	public void testStartsWithIgnoreCase() {
		assertThat(StringUtils.startsWithIgnoreCase(null, null), is(true));
		assertThat(StringUtils.startsWithIgnoreCase(null, "abc"), is(false));
		assertThat(StringUtils.startsWithIgnoreCase("abcdef", null), is(false));
		assertThat(StringUtils.startsWithIgnoreCase("abcdef", "abc"), is(true));
		assertThat(StringUtils.startsWithIgnoreCase("ABCDEF", "abc"), is(true));
	}

– public static boolean startsWithAny(CharSequence sequence, CharSequence… searchStrings)
文字列の先頭が指定文字列のいずれかで始まっていればtrueを返します。

	/**
	 * {@link StringUtils#startsWithAny(CharSequence, CharSequence...)
	 */
	@Test
	public void testStartsWithAny() {
		assertThat(StringUtils.startsWithAny(null, null), is(false));
		assertThat(StringUtils.startsWithAny(null, new String[] { "abc" }), is(false));
		assertThat(StringUtils.startsWithAny("abcxyz", null), is(false));
		assertThat(StringUtils.startsWithAny("abcxyz", new String[] { "" }), is(true));
		assertThat(StringUtils.startsWithAny("abcxyz", new String[] { "abc" }), is(true));
		assertThat(StringUtils.startsWithAny("abcxyz", new String[] { null, "xyz", "abc" }), is(true));
		assertThat(StringUtils.startsWithAny("abcxyz", null, "xyz", "ABCX"), is(false));
		assertThat(StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc"), is(false));
	}

– public static boolean endsWith(CharSequence str, CharSequence suffix)

文字列の末尾が指定文字列で終わっていればtrueを返します。

	/**
	 * {@link StringUtils#endsWith(CharSequence, CharSequence)
	 */
	@Test
	public void testEndsWith() {
		assertThat(StringUtils.endsWith(null, null), is(true));
		assertThat(StringUtils.endsWith(null, "def"), is(false));
		assertThat(StringUtils.endsWith("abcdef", null), is(false));
		assertThat(StringUtils.endsWith("abcdef", "def"), is(true));
		assertThat(StringUtils.endsWith("ABCDEF", "def"), is(false));
		assertThat(StringUtils.endsWith("ABCDEF", "cde"), is(false));
		assertThat(StringUtils.endsWith("ABCDEF", ""), is(true));
	}

– public static boolean endsWithIgnoreCase(CharSequence str, CharSequence prefix)

文字列の末尾が指定文字列で終わっていればtrueを返します。(大文字、小文字の区別なし)

	/**
	 * {@link StringUtils#endsWithIgnoreCase(CharSequence, CharSequence)
	 */
	@Test
	public void testEndsWithIgnoreCase() {
		assertThat(StringUtils.endsWithIgnoreCase(null, null), is(true));
		assertThat(StringUtils.endsWithIgnoreCase(null, "def"), is(false));
		assertThat(StringUtils.endsWithIgnoreCase("abcdef", null), is(false));
		assertThat(StringUtils.endsWithIgnoreCase("abcdef", "def"), is(true));
		assertThat(StringUtils.endsWithIgnoreCase("ABCDEF", "def"), is(true));
		assertThat(StringUtils.endsWithIgnoreCase("ABCDEF", "cde"), is(false));
	}

– public static boolean endsWithAny(CharSequence sequence, CharSequence…searchStrings)

文字列の末尾がが指定文字列のいずれかで終わっていればtrueを返します。

	/**
	 * {@link StringUtils#endsWithAny(CharSequence, CharSequence...)
	 */
	@Test
	public void testEndsWithAny() {
		assertThat(StringUtils.endsWithAny(null, null), is(false));
		assertThat(StringUtils.endsWithAny(null, new String[] { "abc" }), is(false));
		assertThat(StringUtils.endsWithAny("abcxyz", null), is(false));
		assertThat(StringUtils.endsWithAny("abcxyz", new String[] { "" }), is(true));
		assertThat(StringUtils.endsWithAny("abcxyz", new String[] { "xyz" }), is(true));
		assertThat(StringUtils.endsWithAny("abcxyz", new String[] { null, "xyz", "abc" }), is(true));
		assertThat(StringUtils.endsWithAny("abcXYZ", "def", "XYZ"), is(true));
		assertThat(StringUtils.endsWithAny("abcXYZ", "def", "xyz"), is(false));
	}

【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