Play Framework 2 のJava版本をさらっと読んでみた感想
PlayFrameworkのScala版の検証を行っているとDB周りに行き詰まってしまいました。
よくわからないのでそのまま放ったらかしで、PlayFrameworkの本は出てないのかなと調べるとありました!
Play Framework 2徹底入門 JavaではじめるアジャイルWeb開発
- 作者: 掌田津耶乃,原一浩
- 出版社/メーカー: 翔泳社
- 発売日: 2013/12/17
- メディア: 大型本
- この商品を含むブログ (6件) を見る
タイトルをよく見るとJavaと書いてあります。
Java版もどんなものか興味があるので会社で本を買ってみました。
中身をさらっと見るとJava版でも使いやすそうに感じました。
Scala版に比べDB周りはわかりやすい感じで、JPAを知っていればすんなり使えそうでした。
画面周りはJava、Scalaとも同じ仕様なので、Scala版を使っていてわからないことがあった場合、この本を参考にできます。
Scala版を使っている方も持っていて損はない本です。
現時点で「Javaで開発するならこのフレームワーク!」というのはありません。
少し前までならSeasarだったんですが、今あえて言うならPlayFrameworkかなと思ったります。
PlayFrameworkは新しい部類のフレームワークになります。Railsに似たようなものです。
Servlet等のJavaEE関連のAPIは一切使っていないようで、スケールしやすいフレームワークで今の時代に沿ったものだと感じました。
JavaEEに準拠していないというのが吉と出るのか凶と出るのかわかりません。
Javaアプリ全般に言えるのですが、メモリを結構消費します。
本にも書いてますが、PlayFrameworkはメモリ消費が多い部類に入るそうです。
簡単なWebサービスを作って安いVPS等で運用を開始していくと、そのうちにメモリ不足で頭を悩ますことがあるそうですので、その点注意が必要です。
VPSでJavaを動かすなら最低でもメモリ2GBはあった方が良さそうです。
私が開発したデータSIM比較まとめというWebサービスは、Javaで作っていてVPSメモリ1GBで動作しています。
今度アプリを増やしていった時にOutOfMemoryが出てサービス運営に支障が出ないか心配ではあります。
Javaで面倒くさい日付の扱いを簡単にするライブラリ「Joda-Time」 期間編
Joda-Time日付編に続き期間編です。
1記事にまとめようとしましたが、長くなりそうなので分割しました。
日付編はこちらです。shinsuke789.hatenablog.jp
日付処理では時々必要になる期間の操作についてまとめています。
日付の差分や2つの期間の差分を取得することができます。
触っていると各クラスの違いが微妙にわかりにくい点があったので、現時点で使えそうな機能のみ上げています。
Jodaの期間クラス
クラス名 | 機能 |
---|---|
Duration | 日時から日時分秒の差を扱うクラス |
Period | 単純に年月日時分秒毎に日時の差を扱うクラス |
Interval | 2つの期間の差を扱うクラス |
使い方
Duration
// 1日違いで日時を定義 DateTime start = new DateTime("2013-08-01T12:30:50.666"); DateTime end = new DateTime("2013-08-02T12:30:50.666"); // Durationで期間を定義 Duration duration = new Duration(start, end); // --- 期間の差を取得 // ミリ秒で取得 => 86400000 duration.getMilles(); // 日数で取得 => 1 duration.getStandardDays(); // 時数で取得 => 24 duration.getStandardHours(); // 分数で取得 => 1440 duration.getStandardMinutes(); // 秒数で取得 => 86400 duration.getStandardSeconds();
Period
// 日時の定義 DateTime start = new DateTime("2013-08-01T12:30:50.666"); DateTime end = new DateTime("2014-09-08T23:40:00.999"); // Periodで期間を定義 Period period = new Period(start, end); // --- 単純にそれぞれの差を取得する // 年 => 1 p.getYears(); // 月 => 1 p.getMonths(); // 週 => 1 p.getWeeks(); // 日(1周間内の日数) => 0 p.getDays(); // 時 => 11 p.getHours(); // 分 => 9 p.getMinutes(); // 秒 => 10 p.getSeconds(); // ミリ秒 => 333 p.getMillis();
Interval
// 日時と期間の定義1 DateTime start1 = new DateTime("2013-08-01T12:30:30.100"); DateTime end1 = new DateTime("2013-08-10T14:30:50.300"); Interval interval1 = new Interval(start1, end1); // 日時と期間の定義2 DateTime start2 = new DateTime("2013-08-10T12:30:30.100"); DateTime end2 = new DateTime("2013-08-20T14:30:50.300"); Interval interval2 = new Interval(start2, end2); // 2つの期間が連続するか? => true interval1.abuts(interval2); // 2つの期間の差を期間で取得 interval1.gaps(interval2); // 2つの期間の重なった期間を取得 interval1.overlap(interval2); // 期間内に指定の日付が含まれるか => true interval1.contains(new DateTime("2013-08-15")); // 期間内に指定の期間が含まれるか => true interval1.contains(interval2); // 期間内に現在が含まれているか => true interval1.containsNow(); // Durationに変換 interval1.toDuration(); // Periodに変換 interval1.toPeriod();
Intervalのcontains、overlapの時間の有効範囲
有効範囲が微妙に異なるようなのでJodaのJavadocから抜粋しました。
・containsの引数にDateTimeのときの有効範囲
[09:00 to 10:00) contains 08:59 = false (before start)
[09:00 to 10:00) contains 09:00 = true
[09:00 to 10:00) contains 09:59 = true
[09:00 to 10:00) contains 10:00 = false (equals end)
[09:00 to 10:00) contains 10:01 = false (after end)[14:00 to 14:00) contains 14:00 = false (zero duration contains nothing)
・containsの引数にIntervalのときの有効範囲
[09:00 to 10:00) contains [09:00 to 10:00) = true
[09:00 to 10:00) contains [09:00 to 09:30) = true
[09:00 to 10:00) contains [09:30 to 10:00) = true
[09:00 to 10:00) contains [09:15 to 09:45) = true
[09:00 to 10:00) contains [09:00 to 09:00) = true[09:00 to 10:00) contains [08:59 to 10:00) = false (otherStart before thisStart)
[09:00 to 10:00) contains [09:00 to 10:01) = false (otherEnd after thisEnd)
[09:00 to 10:00) contains [10:00 to 10:00) = false (otherStart equals thisEnd)[14:00 to 14:00) contains [14:00 to 14:00) = false (zero duration contains nothing)
・overlapの有効範囲
[09:00 to 10:00) overlaps [08:00 to 08:30) = false (completely before)
[09:00 to 10:00) overlaps [08:00 to 09:00) = false (abuts before)
[09:00 to 10:00) overlaps [08:00 to 09:30) = true
[09:00 to 10:00) overlaps [08:00 to 10:00) = true
[09:00 to 10:00) overlaps [08:00 to 11:00) = true[09:00 to 10:00) overlaps [09:00 to 09:00) = false (abuts before)
[09:00 to 10:00) overlaps [09:00 to 09:30) = true
[09:00 to 10:00) overlaps [09:00 to 10:00) = true
[09:00 to 10:00) overlaps [09:00 to 11:00) = true[09:00 to 10:00) overlaps [09:30 to 09:30) = true
[09:00 to 10:00) overlaps [09:30 to 10:00) = true
[09:00 to 10:00) overlaps [09:30 to 11:00) = true[09:00 to 10:00) overlaps [10:00 to 10:00) = false (abuts after)
[09:00 to 10:00) overlaps [10:00 to 11:00) = false (abuts after)[09:00 to 10:00) overlaps [10:30 to 11:00) = false (completely after)
[14:00 to 14:00) overlaps [14:00 to 14:00) = false (abuts before and after)
[14:00 to 14:00) overlaps [13:00 to 15:00) = true
Javaライブラリの解説を含む本
- 作者: 竹添直樹,島本多可子,小津美夕紀,亀井隆司
- 出版社/メーカー: 翔泳社
- 発売日: 2011/07/16
- メディア: 大型本
- 購入: 6人 クリック: 217回
- この商品を含むブログ (25件) を見る
Javaで面倒くさい日付の扱いを簡単にするライブラリ「Joda-Time」 日付編
Javaで日付を扱うときDateやCalendarを使います。
これらのクラスを使うとき、結構手順を踏まないと目的の日付を取得出来ません。
コードも長くなってメンテナンスもしにくくなります。
そんな問題を解決したのが、日付に特化した「Joda-Time」ライブラリです。
よく使うもので大きく分けると「日付」と「期間」という2つの括りになります。
内容が長くなるのでこの2つで分けて記事にしました。
期間編はこちらです。shinsuke789.hatenablog.jp
日本語のドキュメントがあまりなかったので、時間をかけてまとめてみました。
まとめてみて感じたのが、結構簡単に日付を扱えるということ、作成した日付に対して連続で違う処理を行えることがかなりいいと感じました。
ただ、柔軟すぎてどのメソッドを使ったらいいか迷いますので、ここでは簡潔に書けて必要なものだけ上げました。
目次
必要なライブラリ
joda-time-xxx.jar
今回はバージョン2.2でまとめています。
Jodaの日付クラス
クラス名 | 機能 |
---|---|
DateTime | JavaのCalendarにあたるクラス |
DateTimeMidnight | DateTimeの時間を00:00:00.000に固定したクラス |
LocalDate | ロケールを含まず、日付のみを扱うクラス |
LoclTime | ロケールを含まず、時間のみ扱うクラス |
LocalDateTime | ロケールを含まず、日付と時間を扱うクラス |
サンプルコードではDateTimeを使用していますが、上記クラスでも定義可能です。
使い方
DateTimeの初期化
// 引数なし DateTime dt = new DateTime(); // Date型から DateTime dt = new DateTime(new Date()); // Calendar型から DateTime dt = new DateTime(Calendar.getInstance()); // ISO日付文字列から DateTime dt = new DateTime("2013-08-01"); DateTime dt = new DateTime("2013-08-01T12:30:50") // 年月日時分 DateTime dt = new DateTime(2013, 8, 1, 12, 30); // 年月日時分秒 DateTime dt = new DateTime(2013, 8, 1, 12, 30, 50); // ミリ秒から Date d = new Date(); DateTime dt = new DateTime(d.getTime());
DateTimeからの変換
DateTime dt = new DateTime(); // Calendarに変換 Calendar calendar = dt.toCalendar(null); // Dateに変換 Date date = dt.toDate(); // DateMidnightに変換 DateMidnight dateMidnight = dt.toDateMidnight(); // LocalDateに変換 LocalData localDate = dt.toLocalDate(); // LocalDateTimeに変換 LocalDateTime localDateTime = dt.toLocalDateTime(); // LocalTimeに変換 LocalTime localTime = dt.toLocalTime();
日付の取得
Calendarクラスのgetに相当し、結果を数値で取得する。 // 現在日付取得 // 2013-08-01T12:30:50.666+09:00 DateTime dt = new DateTime(); // 年 => 2013 dt.getYear(); // 月 => 8 dt.getMonthOfYear(); // 日 => 1 dt.getDayOfMonth(); // 曜日(月曜日=1、日曜日=7)=> 4 dt.getDayOfWeek(); // 時 => 12 dt.getHourOfDay(); // 分 => 30 dt.getMinuteOfHour(); // 秒 => 50 dt.getSecondOfMinute(); // ミリ秒 => 666 dt.getMillisOfSecond(); // 1970/1/1からのミリ秒 => 1375327850666 dt.getMillis(); // 月末 => 31(8月の末日) dt.dayOfMonth().getMaximumValue(); // 通年日 => 213 dt.getDayOfYear(); // 通年週 => 31 dt.getWeekOfWeekyear();
日付の再設定
Calendarクラスのsetメソッドに相当し、結果をDateTimeやLocalDateなどのクラスで取得する。
DateTime dt = new DateTime(); // 日付 dt.withDate(2013, 10, 20); // 時刻 dt.withTime(12, 30, 50, 333); // 年 dt.withYear(2012); // 月 dt.withMonthOfYear(5); // 日 dt.withDayOfMonth(10); // 曜日(月曜日=1、日曜日=7) dt.withDayOfWeek(3); // 時 dt.withHourOfDay(12); // 分 dt.withMinuteOfHour(30); // 秒 dt.withSecondOfMinute(50); // ミリ秒 dt.withMillisOfSecond(321); // 1970/1/1からのミリ秒 dt.withMillis(1324543605407); // 通年日 dt.withDayOfYear(34); // 通年週 dt.withWeekOfWeekYear(10);
日付のフォーマット
// 2013-08-01T12:30:50.666+09:00 DateTime dt = new DateTime(); // --- 文字列 // 全て => 2013-08-01T12:30:50.666+09:00 dt.toString(); // 年のみ => 2013 dt.toString("yyyy"); // 月のみ => 08 dt.toString("MM"); // 日のみ => 01 dt.toString("dd"); // 時のみ => 12 dt.toString("HH"); // 分のみ => 30 dt.toString("mm"); // 秒のみ => 50 dt.toString("ss"); // ミリ秒のみ => 666 dt.toString("SSS"); // --- 年月日、時分秒フォーマット // => 2013年8月1日 dt.toString(DateTimeFormat.fullDate()); // => 2013年8月1日 12時30分50秒 JST dt.toString(DateTimeFormat.fullDateTime()); // => 12時30分50秒 JST dt.toString(DateTimeFormat.fullTime()); // --- 「/」「:」フォーマット // => 2013/08/01 dt.toString(DateTimeFormat.longDate()); // => 2013/08/01 12:30:50 JST dt.toString(DateTimeFormat.longDateTime()); // => 12:30:50 JST dt.toString(DateTimeFormat.longTime()); // --- JSTを省略 // => 2013/08/01 dt.toString(DateTimeFormat.mediumDate()); // => 2013/08/01 12:30:50 dt.toString(DateTimeFormat.mediumDateTime()); // => 12:30:50 dt.toString(DateTimeFormat.mediumTime()); // --- 年を2桁または秒を省略 // => 13/08/01 dt.toString(DateTimeFormat.shortDate()); // => 13/08/01 12:30 dt.toString(DateTimeFormat.shortDateTime()); // => 12:30 dt.toString(DateTimeFormat.shortTime());
日付文字列の変換
// --- 文字列からDateTime型へ変換 // yyyy/MM/dd DateTimeFormat.forPattern("yyyy/MM/dd").parseDateTime("2013/08/01"); // スラッシュなし DateTimeFormat.forPattern("yyyyMMdd").parseDateTime("20130801"); // 日時スラッシュなし(CSVなどの文字列でよくあるパターン) DateTimeFormat.forPattern("yyyyMMddHHmmss").parseDateTime("20130801102030"); // --- 文字列からLocalDate型へ変換 DateTimeFormat.forPattern("yyyy/MM/dd").parseLocalDate("2013/08/01"); // --- 文字列からLocalDateTime型へ変換 DateTimeFormat.forPattern("yyyy/MM/dd").parseLocalDateTime("2013/08/01"); // --- 文字列からLocalTime型へ変換 DateTimeFormat.forPattern("HH:mm:ss").parseLocalTime("2013/08/01"); // --- 文字列からDateTime型に変換し、Date型にする DateTimeFormat.forPattern("yyyy/MM/dd").parseDateTime("2013/08/01").toDate(); // --- 文字列からDateTime型に変換し、Calendar型にする DateTimeFormat.forPattern("yyyy/MM/dd").parseDateTime("2013/08/01").toCalendar(Locale.JAPANESE);
日付の比較
// 2013-01-01 00:00:00 DateTime dt1 = new DateTime(2013, 1, 1); // 2013-02-01 00:00:00 DateTime dt2 = new DateTime(2013, 2, 1); // --- 2つのDateTime日時との比較 // dt1はdt2より未来か? => false dt1.isAfter(dt2); // dt1はdt2より過去か? => true dt1.isBefore(dt2); // dt1とdt2は等しいか? => false dt1.isEqual(dt2); // --- 現在日時との比較 // 現在 => 2013-03-01 00:00:00のとき // dt1は現在日時より未来か? => false dt1.isAfterNow(); // dt1は現在日時より過去か? => true dt1.isBeforeNow(); // dt1と現在日時は等しいか? => false dt1.isEqualNow();
日付の加算
設定した値のDateTimeが返ってくるので、再度DateTimeを宣言してから取得する必要があります。
// // 2013-08-01T12:30:50.666+09:00 DateTime dt = new DateTime(); // 月加算 => 2013-10-01T12:30:50.666+09:00 DateTime dt = dt.plusMonths(2); // 日加算 => 2013-08-11T12:30:50.666+09:00 DateTime dt = dt.plusDays(10); // 時加算 => 2013-08-01T16:30:50.666+09:00 DateTime dt = dt.plusHours(4); // 分加算 => 2013-08-01T12:50:50.666+09:00 DateTime dt = dt.plusMinutes(20); // 秒加算 => 2013-08-01T12:31:20.666+09:00 DateTime dt = dt.plusSeconds(30); // ミリ秒加算 => 2013-08-01T12:30:51.166+09:00 DateTime dt = dt.plusMillis(500);
日付の減算
日付の加算と同様に、戻ってきたDateTimeより反映された新しい値を取得します。
DateTime dt = new DateTime(); // 月減算 => 2013-06-01T12:30:50.666+09:00 DateTime dt = dt.minusMonths(2); // 日減算 => 2013-07-22T12:30:50.666+09:00 DateTime dt = dt.minusDays(10); // 時減算 => 2013-08-01T08:30:50.666+09:00 DateTime dt = dt.minusHours(4); // 分減算 => 2013-08-01T12:10:50.666+09:00 DateTime dt = dt.minusMinutes(20); // 秒減算 => 2013-08-01T12:30:20.666+09:00 DateTime dt = dt.minusSeconds(30); // ミリ秒減算 => 2013-08-01T12:30:50.166+09:00 DateTime dt = dt.minusMillis(500);
日付の差分
DateTime dt1 = new DateTime(2013, 5, 10, 20, 40, 30); DateTime dt2 = new DateTime(2015, 7, 1, 12, 30, 10); // 年 => 2 int year = Years.yearsBetween(dt1, dt2).getYears(); // 月 => 25 int month = Months.monthsBetween(dt1, dt2).getMonths(); // 日 => 781 int day = Days.daysBetween(dt1, dt2).getDays(); // 時 => 18759 int hour = Hours.hoursBetween(dt1, dt2).getHours(); // 分 => 1125589 int minute = Minutes.minutesBetween(dt1, dt2).getMinutes(); // 秒 => 67535380 int second = Seconds.secondsBetween(dt1, dt2).getSeconds();
システム日付
// 現在日時を取得する DateTimeUtils.currentTimeMillis() // 現在日時を指定のミリ秒で強制的に変更する DateTimeUtils.setCurrentMillisFixed(millis); // 現在日時を現在の時刻からのミリ秒オフセットで強制的に変更する DateTimeUtils.setCurrentMillisOffset(millis); // 強制変更した現在日時をリセットする DateTimeUtils.setCurrentMillisSystem();
定数
DateTimeConstantsが定数クラス。
// --- 曜日 // 月曜(1) DateTimeConstants.MONDAY // 火曜(2) DateTimeConstants.TUESDAY // 水曜(3) DateTimeConstants.WEDNESDAY // 木曜(4) DateTimeConstants.THURSDAY // 金曜(5) DateTimeConstants.FRIDAY // 土曜(6) DateTimeConstants.SATURDAY // 日曜(7) DateTimeConstants.SUNDAY // --- 月 // 1月 DateTimeConstants.JANUARY // 2月 DateTimeConstants.FEBRUARY // 3月 DateTimeConstants.MARCH // 4月 DateTimeConstants.APRIL // 5月 DateTimeConstants.MAY // 6月 DateTimeConstants.JUNE // 7月 DateTimeConstants.JULY // 8月 DateTimeConstants.AUGUST // 9月 DateTimeConstants.SEPTEMBER // 10月 DateTimeConstants.OCTOBER // 11月 DateTimeConstants.NOVEMBER // 12月 DateTimeConstants.DECEMBER
Javaライブラリの解説を含む本
- 作者: 竹添直樹,島本多可子,小津美夕紀,亀井隆司
- 出版社/メーカー: 翔泳社
- 発売日: 2011/07/16
- メディア: 大型本
- 購入: 6人 クリック: 217回
- この商品を含むブログ (25件) を見る
SLF4Jで出力されるワーニングを消す
commons-loggingを使っているプログラムでSLF4Jを使用していると次のワーニングが出ることがあります。
WARN: The method class org.apache.commons.logging.impl.SLF4JLogFactory#release() was invoked. WARN: Please see http://www.slf4j.org/codes.html#release for an explanation.
この時はライブラリの読み込みでcommons-logging.jarが邪魔をしているようなのでこれを削除するとワーニングが出なくなります。
Apache commons-langのBooleanUtilsが便利なのでまとめてみた
commons-langのBooleanUtilsにbooleanを文字列や数値で返したり、またその逆を行ったりする機能がありました。
小さな事ですけど結構便利なものが提供されています。使わない理由はないでしょう。
ということでまとめてみました。
プリミティブ型とオブジェクト型を考慮したメソッドもありますが、
Java5以降であればオートボクシングで自動で変換してくれるので気にする必要はないでしょう。
バージョン
commns-lang 3.1
negate
boolan値を否定する。true→false、false→true
BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE; BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE; BooleanUtils.negate(null) = null;
isTrue
trueならtrueを返す
BooleanUtils.isTrue(Boolean.TRUE) = true BooleanUtils.isTrue(Boolean.FALSE) = false BooleanUtils.isTrue(null) = false
isNotTrue
trueでないならfalseを返す
BooleanUtils.isNotTrue(Boolean.TRUE) = false BooleanUtils.isNotTrue(Boolean.FALSE) = true BooleanUtils.isNotTrue(null) = tru
isFalse
falseならfalseを返す
BooleanUtils.isFalse(Boolean.TRUE) = false BooleanUtils.isFalse(Boolean.FALSE) = true BooleanUtils.isFalse(null) = false
isNotFalse
falseでないならtrueを返す
BooleanUtils.isNotFalse(Boolean.TRUE) = true BooleanUtils.isNotFalse(Boolean.FALSE) = false BooleanUtils.isNotFalse(null) = true
toBoolean
別の型の真偽値をbooleanで返す
// int 0→false、1以上→ture BooleanUtils.toBoolean(0) = false BooleanUtils.toBoolean(1) = true BooleanUtils.toBoolean(2) = true // string true/yes/on→true、false/no/off→false BooleanUtils.toBoolean(null) = false BooleanUtils.toBoolean("true") = true BooleanUtils.toBoolean("TRUE") = true BooleanUtils.toBoolean("tRUe") = true BooleanUtils.toBoolean("on") = true BooleanUtils.toBoolean("yes") = true BooleanUtils.toBoolean("false") = false BooleanUtils.toBoolean("x gti") = false // intで真偽値の条件設定 第2引数にtrueのint値、第3引数にfalseのint値 BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false BooleanUtils.toBoolean(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(0)) = false BooleanUtils.toBoolean(Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(0)) = true BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2)) = false BooleanUtils.toBoolean(Integer.valueOf(2), Integer.valueOf(2), Integer.valueOf(0)) = true BooleanUtils.toBoolean(null, null, Integer.valueOf(0)) = true // stringで真偽値の条件設定 第2引数にtrueのstring値、第3引数にfalseのstring値 BooleanUtils.toBoolean("true", "true", "false") = true BooleanUtils.toBoolean("false", "true", "false") = false BooleanUtils.toBoolean("A", "A", "B") = true
toBooleanDefaultIfNull
nullの場合のデフォルト値を設定して返す
BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false BooleanUtils.toBooleanDefaultIfNull(null, true) = true
toInteger
booleanをintegerで返す
// booleanをtrueに変換 BooleanUtils.toInteger(true) = 1 BooleanUtils.toInteger(false) = 0 // 変換条件設定 第2引数にtrueのときの値、第3引数にfalseのときの値 BooleanUtils.toInteger(true, 1, 0) = 1 BooleanUtils.toInteger(false, 1, 0) = 0 // 変換条件、null時の変換設定 第2引数にtrue時の値、第3引数にfalse時の値 第4引数にnull時の値 BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1 BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0 BooleanUtils.toInteger(null, 1, 0, 2) = 2
toString
booleanをStringで返す
// 変換条件設定 第2引数にtrueのときの値、第3引数にfalseのときの値 BooleanUtils.toString(true, "true", "false") = "true" BooleanUtils.toString(false, "true", "false") = "false" // 変換条件、null時の変換設定 第2引数にtrue時の値、第3引数にfalse時の値 第4引数にnull時の値 BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true" BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false" BooleanUtils.toString(null, "true", "false", null) = null;
toStringOnOff
true→on、false→offを文字列で返す
BooleanUtils.toStringOnOff(true) = "on" BooleanUtils.toStringOnOff(false) = "off"
toStringYesNo
true→yes、false→noを文字列で返す
BooleanUtils.toStringOnOff(true) = "yes" BooleanUtils.toStringOnOff(false) = "no"
toStringTrueFalse
true→true、false→falseを文字列で返す
BooleanUtils.toStringTrueFalse(true) = "true" BooleanUtils.toStringTrueFalse(false) = "false"
参考
BooleanUtils (Commons Lang 3.1 API)
Apache commons-langのStringUtilsで使えそうなものを抜き出してみた
commons-lang3.1のAPIから気になるものだけ抜き出したので、他に記載してないものもあります。
サンプルコードはAPIのコピペです。
文字列に関するよく使うロジックがまとまっているクラスです。
文字列をnullで比較するとぬるぽで落ちてしまうのですが、このクラスのものを使うとぬるぽで落ちなくなります。
if(hoge != null)という冗長なコードがなくなってスッキリします。
まとめていると同じようなものがあったので使い分けはどうするのか不明です。
若干処理内容が異なるかもしれませんのでその点は注意して下さい。
バージョン
commns-lang 3.1目次
- 判定
- isEmpty:nullまたは空文字のときtrueを返す
- isNotEmpty:nullまたは空文字ではないときtrueを返す
- isBlank:空文字ならtrueを返す
- isNotBlank:空文字ではないならtrueを返す
- isAlpha:アルファベットならtrueを返す
- isAlphaSpace:アルファベットまたは空白ならtrueを返す
- isAlphanumeric:アルファベットまたは数字ならtrueを返す
- isAlphanumericSpace:アルファベットまたは数字または空白ならtrueを返す
- isNumeric:数字ならtrueを返す
- isNumericSpace:数値または空白ならtrueを返す
- isWhitespace:空白ならtrueを返す
- isAllLowerCase:全て小文字ならtrueを返す
- isAllUpperCase:全て大文字ならtrueを返す
- startsWith:文字列の先頭が指定文字列で始まっていればtrueを返す
- startsWithIgnoreCase:大文字・小文字を区別せずに文字列の先頭が指定文字列で始まっていればtrueを返す
- startsWithAny:文字列の先頭が指定文字列のいずれかで始まっていればtrueを返す
- endsWith:文字列の末尾が指定文字列で終わっていればtrueを返す
- endsWithIgnoreCase:大文字・小文字を区別せずに文字列の末尾が指定文字列で終わっていればtrueを返す
- endsWithAny:文字列の末尾が指定文字列のいずれかで終わっていればtrueを返す
- 除去・削除
- trim:文字列の先頭、末尾から制御文字を除去する
- trimToNull:文字列の先頭、末尾から制御文字を除去し、結果が空文字ならnullを返す
- trimToEmpty:文字列の先頭、末尾から制御文字を除去し、結果が空文字なら空文字を返す
- strip:文字列の先頭、末尾の空白を除去する 指定文字でも除去可能
- stripToNull:文字列の先頭、末尾の空白を除去し、結果が空文字ならnullを返す
- stripToEmpty:文字列の先頭、末尾の空白を除去し、結果がnullなら空文字を返す
- stripStart:文字列の先頭から指定文字を除去する
- stripEnd:文字列の末尾から指定文字を除去する
- stripAll:複数の文字列から空白を除去する 指定文字でも可能
- deleteWhitespace:空白を削除する
- removeStart:文字列の先頭を指定文字で削除する
- removeStartIgnoreCase:大文字・小文字を区別せずに文字列の先頭を指定文字で削除する
- removeEnd:文字列の末尾を指定文字で削除する
- removeEndIgnoreCase:大文字・小文字を区別せずに文字列の末尾を指定文字で削除する
- remove:指定文字を削除する
- chomp:文字列末尾の改行コードを削除する
- 比較
- 検索
- 切り出し
- substring:nullを考慮して指定位置の文字列を切り出す
- left:文字列の先頭から指定文字数を切り出す
- right:文字列の末尾から指定文字数を切り出す
- mid:開始位置から指定文字数を切り出す
- substringBefore:指定文字が最初に発見された文字より前の文字を切り出す
- substringAfter:指定文字が最初に発見された文字より後の文字を切り出す
- substringBeforeLast:指定文字が最後に発見された文字より前の文字を切り出す
- substringAfterLast:指定文字が最後に発見された文字より後の文字を切り出す
- split:指定文字で文字列を分割する
- 変換
isEmpty
nullまたは空文字のときtrueを返すStringUtils.isEmpty(null) = true StringUtils.isEmpty("") = true StringUtils.isEmpty(" ") = false StringUtils.isEmpty("bob") = false StringUtils.isEmpty(" bob ") = false
isNotEmpty
nullまたは空文字ではないときtrueを返すStringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = false StringUtils.isNotEmpty(" ") = true StringUtils.isNotEmpty("bob") = true StringUtils.isNotEmpty(" bob ") = true
isBlank
空文字ならtrueを返すStringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false
isNotBlank
空文字ではないならtrueを返すStringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true
isAlpha
アルファベットならtrueを返すStringUtils.isAlpha(null) = false StringUtils.isAlpha("") = false StringUtils.isAlpha(" ") = false StringUtils.isAlpha("abc") = true StringUtils.isAlpha("ab2c") = false StringUtils.isAlpha("ab-c") = false
isAlphaSpace
アルファベットまたは空白ならtrueを返すStringUtils.isAlphaSpace(null) = false StringUtils.isAlphaSpace("") = true StringUtils.isAlphaSpace(" ") = true StringUtils.isAlphaSpace("abc") = true StringUtils.isAlphaSpace("ab c") = true StringUtils.isAlphaSpace("ab2c") = false StringUtils.isAlphaSpace("ab-c") = false
StringUtils.isAlphanumeric(null) = false StringUtils.isAlphanumeric("") = false StringUtils.isAlphanumeric(" ") = false StringUtils.isAlphanumeric("abc") = true StringUtils.isAlphanumeric("ab c") = false StringUtils.isAlphanumeric("ab2c") = true StringUtils.isAlphanumeric("ab-c") = false
isAlphanumericSpace
アルファベットまたは数字または空白ならtrueを返すStringUtils.isAlphanumericSpace(null) = false StringUtils.isAlphanumericSpace("") = true StringUtils.isAlphanumericSpace(" ") = true StringUtils.isAlphanumericSpace("abc") = true StringUtils.isAlphanumericSpace("ab c") = true StringUtils.isAlphanumericSpace("ab2c") = true StringUtils.isAlphanumericSpace("ab-c") = false
isNumeric
数字ならtrueを返すStringUtils.isNumeric(null) = false StringUtils.isNumeric("") = false StringUtils.isNumeric(" ") = false StringUtils.isNumeric("123") = true StringUtils.isNumeric("12 3") = false StringUtils.isNumeric("ab2c") = false StringUtils.isNumeric("12-3") = false StringUtils.isNumeric("12.3") = false
isNumericSpace
数値または空白ならtrueを返すStringUtils.isNumericSpace(null) = false StringUtils.isNumericSpace("") = true StringUtils.isNumericSpace(" ") = true StringUtils.isNumericSpace("123") = true StringUtils.isNumericSpace("12 3") = true StringUtils.isNumericSpace("ab2c") = false StringUtils.isNumericSpace("12-3") = false StringUtils.isNumericSpace("12.3") = false
isWhitespace
空白ならtrueを返すStringUtils.isWhitespace(null) = false StringUtils.isWhitespace("") = true StringUtils.isWhitespace(" ") = true StringUtils.isWhitespace("abc") = false StringUtils.isWhitespace("ab2c") = false StringUtils.isWhitespace("ab-c") = false
isAllLowerCase
全て小文字ならtrueを返すStringUtils.isAllLowerCase(null) = false StringUtils.isAllLowerCase("") = false StringUtils.isAllLowerCase(" ") = false StringUtils.isAllLowerCase("abc") = true StringUtils.isAllLowerCase("abC") = false
isAllUpperCase
全て大文字ならtrueを返すStringUtils.isAllUpperCase(null) = false StringUtils.isAllUpperCase("") = false StringUtils.isAllUpperCase(" ") = false StringUtils.isAllUpperCase("ABC") = true StringUtils.isAllUpperCase("aBC") = false
startsWith
文字列の先頭が指定文字列で始まっていればtrueを返すStringUtils.startsWith(null, null) = true StringUtils.startsWith(null, "abc") = false StringUtils.startsWith("abcdef", null) = false StringUtils.startsWith("abcdef", "abc") = true StringUtils.startsWith("ABCDEF", "abc") = false
startsWithIgnoreCase
大文字・小文字を区別せずに文字列の先頭が指定文字列で始まっていればtrueを返すStringUtils.startsWithIgnoreCase(null, null) = true StringUtils.startsWithIgnoreCase(null, "abc") = false StringUtils.startsWithIgnoreCase("abcdef", null) = false StringUtils.startsWithIgnoreCase("abcdef", "abc") = true StringUtils.startsWithIgnoreCase("ABCDEF", "abc") = true
startsWithAny
文字列の先頭が指定文字列のいずれかで始まっていればtrueを返すStringUtils.startsWithAny(null, null) = false StringUtils.startsWithAny(null, new String[] {"abc"}) = false StringUtils.startsWithAny("abcxyz", null) = false StringUtils.startsWithAny("abcxyz", new String[] {""}) = false StringUtils.startsWithAny("abcxyz", new String[] {"abc"}) = true StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
endsWith
文字列の末尾が指定文字列で終わっていればtrueを返すStringUtils.endsWith(null, null) = true StringUtils.endsWith(null, "def") = false StringUtils.endsWith("abcdef", null) = false StringUtils.endsWith("abcdef", "def") = true StringUtils.endsWith("ABCDEF", "def") = false StringUtils.endsWith("ABCDEF", "cde") = false
endsWithIgnoreCase
大文字・小文字を区別せずに文字列の末尾が指定文字列で終わっていればtrueを返すStringUtils.endsWithIgnoreCase(null, null) = true StringUtils.endsWithIgnoreCase(null, "def") = false StringUtils.endsWithIgnoreCase("abcdef", null) = false StringUtils.endsWithIgnoreCase("abcdef", "def") = true StringUtils.endsWithIgnoreCase("ABCDEF", "def") = true StringUtils.endsWithIgnoreCase("ABCDEF", "cde") = false
endsWithAny
文字列の末尾が指定文字列のいずれかで終わっていればtrueを返すStringUtils.endsWithAny(null, null) = false StringUtils.endsWithAny(null, new String[] {"abc"}) = false StringUtils.endsWithAny("abcxyz", null) = false StringUtils.endsWithAny("abcxyz", new String[] {""}) = true StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
trim
文字列の先頭、末尾から制御文字を除去するStringUtils.trim(null) = null StringUtils.trim("") = "" StringUtils.trim(" ") = "" StringUtils.trim("abc") = "abc" StringUtils.trim(" abc ") = "abc"
trimToNull
文字列の先頭、末尾から制御文字を除去し、結果が空文字ならnullを返すStringUtils.trimToNull(null) = null StringUtils.trimToNull("") = null StringUtils.trimToNull(" ") = null StringUtils.trimToNull("abc") = "abc" StringUtils.trimToNull(" abc ") = "abc"
trimToEmpty
文字列の先頭、末尾から制御文字を除去し、結果が空文字なら空文字を返すStringUtils.trimToEmpty(null) = "" StringUtils.trimToEmpty("") = "" StringUtils.trimToEmpty(" ") = "" StringUtils.trimToEmpty("abc") = "abc" StringUtils.trimToEmpty(" abc ") = "abc"
strip
文字列の先頭、末尾の空白を除去する 指定文字でも除去可能// 空白を除去 StringUtils.strip(null) = null StringUtils.strip("") = "" StringUtils.strip(" ") = "" StringUtils.strip("abc") = "abc" StringUtils.strip(" abc") = "abc" StringUtils.strip("abc ") = "abc" StringUtils.strip(" abc ") = "abc" StringUtils.strip(" ab c ") = "ab c" // 指定文字で除去 StringUtils.strip(null, *) = null StringUtils.strip("", *) = "" StringUtils.strip("abc", null) = "abc" StringUtils.strip(" abc", null) = "abc" StringUtils.strip("abc ", null) = "abc" StringUtils.strip(" abc ", null) = "abc" StringUtils.strip(" abcyx", "xyz") = " abc"
stripToNull
文字列の先頭、末尾の空白を除去し、結果が空文字ならnullを返すStringUtils.stripToNull(null) = null StringUtils.stripToNull("") = null StringUtils.stripToNull(" ") = null StringUtils.stripToNull("abc") = "abc" StringUtils.stripToNull(" abc") = "abc" StringUtils.stripToNull("abc ") = "abc" StringUtils.stripToNull(" abc ") = "abc" StringUtils.stripToNull(" ab c ") = "ab c"
stripToEmpty
文字列の先頭、末尾の空白を除去し、結果がnullなら空文字を返すStringUtils.stripToEmpty(null) = "" StringUtils.stripToEmpty("") = "" StringUtils.stripToEmpty(" ") = "" StringUtils.stripToEmpty("abc") = "abc" StringUtils.stripToEmpty(" abc") = "abc" StringUtils.stripToEmpty("abc ") = "abc" StringUtils.stripToEmpty(" abc ") = "abc" StringUtils.stripToEmpty(" ab c ") = "ab c"
stripStart
文字列の先頭から指定文字を除去するStringUtils.stripStart(null, *) = null StringUtils.stripStart("", *) = "" StringUtils.stripStart("abc", "") = "abc" StringUtils.stripStart("abc", null) = "abc" StringUtils.stripStart(" abc", null) = "abc" StringUtils.stripStart("abc ", null) = "abc " StringUtils.stripStart(" abc ", null) = "abc " StringUtils.stripStart("yxabc ", "xyz") = "abc "
stripEnd
文字列の末尾から指定文字を除去するStringUtils.stripEnd(null, *) = null StringUtils.stripEnd("", *) = "" StringUtils.stripEnd("abc", "") = "abc" StringUtils.stripEnd("abc", null) = "abc" StringUtils.stripEnd(" abc", null) = " abc" StringUtils.stripEnd("abc ", null) = "abc" StringUtils.stripEnd(" abc ", null) = " abc" StringUtils.stripEnd(" abcyx", "xyz") = " abc" StringUtils.stripEnd("120.00", ".0") = "12"
stripAll
複数の文字列から空白を除去する 指定文字でも可能StringUtils.stripAll(null) = null StringUtils.stripAll([]) = [] StringUtils.stripAll(["abc", " abc"]) = ["abc", "abc"] StringUtils.stripAll(["abc ", null]) = ["abc", null] // 指定文字を除去 StringUtils.stripAll(null, *) = null StringUtils.stripAll([], *) = [] StringUtils.stripAll(["abc", " abc"], null) = ["abc", "abc"] StringUtils.stripAll(["abc ", null], null) = ["abc", null] StringUtils.stripAll(["abc ", null], "yz") = ["abc ", null] StringUtils.stripAll(["yabcz", null], "yz") = ["abc", null]
deleteWhitespace
空白を削除するStringUtils.deleteWhitespace(null) = null StringUtils.deleteWhitespace("") = "" StringUtils.deleteWhitespace("abc") = "abc" StringUtils.deleteWhitespace(" ab c ") = "abc"
removeStart
文字列の先頭を指定文字で削除するStringUtils.removeStart(null, *) = null StringUtils.removeStart("", *) = "" StringUtils.removeStart(*, null) = * StringUtils.removeStart("www.domain.com", "www.") = "domain.com" StringUtils.removeStart("domain.com", "www.") = "domain.com" StringUtils.removeStart("www.domain.com", "domain") = "www.domain.com" StringUtils.removeStart("abc", "") = "abc"
removeStartIgnoreCase
大文字・小文字を区別せずに文字列の先頭を指定文字で削除するStringUtils.removeStartIgnoreCase(null, *) = null StringUtils.removeStartIgnoreCase("", *) = "" StringUtils.removeStartIgnoreCase(*, null) = * StringUtils.removeStartIgnoreCase("www.domain.com", "www.") = "domain.com" StringUtils.removeStartIgnoreCase("www.domain.com", "WWW.") = "domain.com" StringUtils.removeStartIgnoreCase("domain.com", "www.") = "domain.com" StringUtils.removeStartIgnoreCase("www.domain.com", "domain") = "www.domain.com" StringUtils.removeStartIgnoreCase("abc", "") = "abc"
removeEnd
文字列の末尾を指定文字で削除するStringUtils.removeEnd(null, *) = null StringUtils.removeEnd("", *) = "" StringUtils.removeEnd(*, null) = * StringUtils.removeEnd("www.domain.com", ".com.") = "www.domain.com" StringUtils.removeEnd("www.domain.com", ".com") = "www.domain" StringUtils.removeEnd("www.domain.com", "domain") = "www.domain.com" StringUtils.removeEnd("abc", "") = "abc"
removeEndIgnoreCase
大文字・小文字を区別せずに文字列の末尾を指定文字で削除するStringUtils.removeEndIgnoreCase(null, *) = null StringUtils.removeEndIgnoreCase("", *) = "" StringUtils.removeEndIgnoreCase(*, null) = * StringUtils.removeEndIgnoreCase("www.domain.com", ".com.") = "www.domain.com" StringUtils.removeEndIgnoreCase("www.domain.com", ".com") = "www.domain" StringUtils.removeEndIgnoreCase("www.domain.com", "domain") = "www.domain.com" StringUtils.removeEndIgnoreCase("abc", "") = "abc" StringUtils.removeEndIgnoreCase("www.domain.com", ".COM") = "www.domain") StringUtils.removeEndIgnoreCase("www.domain.COM", ".com") = "www.domain")
remove
指定文字を削除するStringUtils.remove(null, *) = null StringUtils.remove("", *) = "" StringUtils.remove(*, null) = * StringUtils.remove(*, "") = * StringUtils.remove("queued", "ue") = "qd" StringUtils.remove("queued", "zz") = "queued"
chomp
文字列末尾の改行コードを削除するStringUtils.chomp(null) = null StringUtils.chomp("") = "" StringUtils.chomp("abc \r") = "abc " StringUtils.chomp("abc\n") = "abc" StringUtils.chomp("abc\r\n") = "abc" StringUtils.chomp("abc\r\n\r\n") = "abc\r\n" StringUtils.chomp("abc\n\r") = "abc\n" StringUtils.chomp("abc\n\rabc") = "abc\n\rabc" StringUtils.chomp("\r") = "" StringUtils.chomp("\n") = "" StringUtils.chomp("\r\n") = ""
equals
nullを考慮して文字列を比較するStringUtils.equals(null, null) = true StringUtils.equals(null, "abc") = false StringUtils.equals("abc", null) = false StringUtils.equals("abc", "abc") = true StringUtils.equals("abc", "ABC") = false
equalsIgnoreCase
nullを考慮し大文字・小文字を区別せずに文字列を比較するStringUtils.equalsIgnoreCase(null, null) = true StringUtils.equalsIgnoreCase(null, "abc") = false StringUtils.equalsIgnoreCase("abc", null) = false StringUtils.equalsIgnoreCase("abc", "abc") = true StringUtils.equalsIgnoreCase("abc", "ABC") = true
difference
文字列を比較し異なる部分を返すStringUtils.difference(null, null) = null StringUtils.difference("", "") = "" StringUtils.difference("", "abc") = "abc" StringUtils.difference("abc", "") = "" StringUtils.difference("abc", "abc") = "" StringUtils.difference("ab", "abxyz") = "xyz" StringUtils.difference("abcde", "abxyz") = "xyz" StringUtils.difference("abcde", "xyz") = "xyz"
indexOfDifference
文字列を比較し異なる部分の開始位置を返すStringUtils.indexOfDifference(null, null) = -1 StringUtils.indexOfDifference("", "") = -1 StringUtils.indexOfDifference("", "abc") = 0 StringUtils.indexOfDifference("abc", "") = 0 StringUtils.indexOfDifference("abc", "abc") = -1 StringUtils.indexOfDifference("ab", "abxyz") = 2 StringUtils.indexOfDifference("abcde", "abxyz") = 2 StringUtils.indexOfDifference("abcde", "xyz") = 0
indexOf
指定文字列の出現位置を返す nullの場合-1を返すStringUtils.indexOf(null, *) = -1 StringUtils.indexOf("", *) = -1 StringUtils.indexOf("aabaabaa", 'a') = 0 StringUtils.indexOf("aabaabaa", 'b') = 2
contains
指定文字列を含めばtureを返す nullの場合falseを返すStringUtils.contains(null, *) = false StringUtils.contains("", *) = false StringUtils.contains("abc", 'a') = true StringUtils.contains("abc", 'z') = false
containsIgnoreCase
大文字・小文字を区別せず指定文字列を含めばtrueを返す nullの場合falseを返すStringUtils.contains(null, *) = false StringUtils.contains(*, null) = false StringUtils.contains("", "") = true StringUtils.contains("abc", "") = true StringUtils.contains("abc", "a") = true StringUtils.contains("abc", "z") = false StringUtils.contains("abc", "A") = true StringUtils.contains("abc", "Z") = false
containsWhitespace
空白を含んでいればtrueを返すStringUtils.containsWhitespace(" aa a a") = true StringUtils.containsWhitespace(null) = false StringUtils.containsWhitespace("aaaaa") = false
containsNone
文字列を含んでいなければtrueを返すStringUtils.containsNone(null, *) = true StringUtils.containsNone(*, null) = true StringUtils.containsNone("", *) = true StringUtils.containsNone("ab", "") = true StringUtils.containsNone("abab", "xyz") = true StringUtils.containsNone("ab1", "xyz") = true StringUtils.containsNone("abz", "xyz") = false
substring
nullを考慮して指定位置の文字列を切り出す// 開始位置以降を切り出す StringUtils.substring(null, *) = null StringUtils.substring("", *) = "" StringUtils.substring("abc", 0) = "abc" StringUtils.substring("abc", 2) = "c" StringUtils.substring("abc", 4) = "" StringUtils.substring("abc", -2) = "bc" StringUtils.substring("abc", -4) = "abc" // 開始位置から終了位置までを切り出す StringUtils.substring(null, *, *) = null StringUtils.substring("", * , *) = ""; StringUtils.substring("abc", 0, 2) = "ab" StringUtils.substring("abc", 2, 0) = "" StringUtils.substring("abc", 2, 4) = "c" StringUtils.substring("abc", 4, 6) = "" StringUtils.substring("abc", 2, 2) = "" StringUtils.substring("abc", -2, -1) = "b" StringUtils.substring("abc", -4, 2) = "ab"
left
文字列の先頭から指定文字数を切り出すStringUtils.left(null, *) = null StringUtils.left(*, -ve) = "" StringUtils.left("", *) = "" StringUtils.left("abc", 0) = "" StringUtils.left("abc", 2) = "ab" StringUtils.left("abc", 4) = "abc"
right
文字列の末尾から指定文字数を切り出すStringUtils.right(null, *) = null StringUtils.right(*, -ve) = "" StringUtils.right("", *) = "" StringUtils.right("abc", 0) = "" StringUtils.right("abc", 2) = "bc" StringUtils.right("abc", 4) = "abc"
mid
開始位置から指定文字数を切り出すStringUtils.mid(null, *, *) = null StringUtils.mid(*, *, -ve) = "" StringUtils.mid("", 0, *) = "" StringUtils.mid("abc", 0, 2) = "ab" StringUtils.mid("abc", 0, 4) = "abc" StringUtils.mid("abc", 2, 4) = "c" StringUtils.mid("abc", 4, 2) = "" StringUtils.mid("abc", -2, 2) = "ab"
substringBefore
指定文字が最初に発見された文字より前の文字を切り出すStringUtils.substringBefore(null, *) = null StringUtils.substringBefore("", *) = "" StringUtils.substringBefore("abc", "a") = "" StringUtils.substringBefore("abcba", "b") = "a" StringUtils.substringBefore("abc", "c") = "ab" StringUtils.substringBefore("abc", "d") = "abc" StringUtils.substringBefore("abc", "") = "" StringUtils.substringBefore("abc", null) = "abc"
substringAfter
指定文字が最初に発見された文字より後の文字を切り出すStringUtils.substringAfter(null, *) = null StringUtils.substringAfter("", *) = "" StringUtils.substringAfter(*, null) = "" StringUtils.substringAfter("abc", "a") = "bc" StringUtils.substringAfter("abcba", "b") = "cba" StringUtils.substringAfter("abc", "c") = "" StringUtils.substringAfter("abc", "d") = "" StringUtils.substringAfter("abc", "") = "abc"
substringBeforeLast
指定文字が最後に発見された文字より前の文字を切り出すStringUtils.substringBeforeLast(null, *) = null StringUtils.substringBeforeLast("", *) = "" StringUtils.substringBeforeLast("abcba", "b") = "abc" StringUtils.substringBeforeLast("abc", "c") = "ab" StringUtils.substringBeforeLast("a", "a") = "" StringUtils.substringBeforeLast("a", "z") = "a" StringUtils.substringBeforeLast("a", null) = "a" StringUtils.substringBeforeLast("a", "") = "a"
substringAfterLast
指定文字が最後に発見された文字より後の文字を切り出すStringUtils.substringAfterLast(null, *) = null StringUtils.substringAfterLast("", *) = "" StringUtils.substringAfterLast(*, "") = "" StringUtils.substringAfterLast(*, null) = "" StringUtils.substringAfterLast("abc", "a") = "bc" StringUtils.substringAfterLast("abcba", "b") = "a" StringUtils.substringAfterLast("abc", "c") = "" StringUtils.substringAfterLast("a", "a") = "" StringUtils.substringAfterLast("a", "z") = ""
split
指定文字で文字列を分割する// 空白で切り出す StringUtils.split(null) = null StringUtils.split("") = [] StringUtils.split("abc def") = ["abc", "def"] StringUtils.split("abc def") = ["abc", "def"] StringUtils.split(" abc ") = ["abc" // 指定文字で切り出す StringUtils.split(null, *) = null StringUtils.split("", *) = [] StringUtils.split("a.b.c", '.') = ["a", "b", "c"] StringUtils.split("a..b.c", '.') = ["a", "b", "c"] StringUtils.split("a:b:c", '.') = ["a:b:c"] StringUtils.split("a b c", ' ') = ["a", "b", "c"]
join
文字列を結合する// 単純に結合する StringUtils.join(null) = null StringUtils.join([]) = "" StringUtils.join([null]) = "" StringUtils.join(["a", "b", "c"]) = "abc" StringUtils.join([null, "", "a"]) = "a" // 指定文字を挟んで結合する StringUtils.join(null, *) = null StringUtils.join([], *) = "" StringUtils.join([null], *) = "" StringUtils.join(["a", "b", "c"], ';') = "a;b;c" StringUtils.join(["a", "b", "c"], null) = "abc" StringUtils.join([null, "", "a"], ';') = ";;a"
replaceOnce
検索文字を指定文字で1つだけ置換するStringUtils.replaceOnce(null, *, *) = null StringUtils.replaceOnce("", *, *) = "" StringUtils.replaceOnce("any", null, *) = "any" StringUtils.replaceOnce("any", *, null) = "any" StringUtils.replaceOnce("any", "", *) = "any" StringUtils.replaceOnce("aba", "a", null) = "aba" StringUtils.replaceOnce("aba", "a", "") = "ba" StringUtils.replaceOnce("aba", "a", "z") = "zba"
replace
検索文字を指定文字に全て置換するStringUtils.replace(null, *, *) = null StringUtils.replace("", *, *) = "" StringUtils.replace("any", null, *) = "any" StringUtils.replace("any", *, null) = "any" StringUtils.replace("any", "", *) = "any" StringUtils.replace("aba", "a", null) = "aba" StringUtils.replace("aba", "a", "") = "b" StringUtils.replace("aba", "a", "z") = "zbz"
repeat
指定文字を指定回数分繰り返すStringUtils.repeat(null, 2) = null StringUtils.repeat("", 0) = "" StringUtils.repeat("", 2) = "" StringUtils.repeat("a", 3) = "aaa" StringUtils.repeat("ab", 2) = "abab" StringUtils.repeat("a", -2) = ""
rightPad
文字列の末尾に指定桁数を最大として文字列を追加する// 空白を追加する StringUtils.rightPad(null, *) = null StringUtils.rightPad("", 3) = " " StringUtils.rightPad("bat", 3) = "bat" StringUtils.rightPad("bat", 5) = "bat " StringUtils.rightPad("bat", 1) = "bat" StringUtils.rightPad("bat", -1) = "bat" // 指定文字を追加する StringUtils.rightPad(null, *, *) = null StringUtils.rightPad("", 3, 'z') = "zzz" StringUtils.rightPad("bat", 3, 'z') = "bat" StringUtils.rightPad("bat", 5, 'z') = "batzz" StringUtils.rightPad("bat", 1, 'z') = "bat" StringUtils.rightPad("bat", -1, 'z') = "bat"
leftPad
文字列の先頭に指定桁数を最大として文字列を追加する// 空白を追加する StringUtils.leftPad(null, *) = null StringUtils.leftPad("", 3) = " " StringUtils.leftPad("bat", 3) = "bat" StringUtils.leftPad("bat", 5) = " bat" StringUtils.leftPad("bat", 1) = "bat" StringUtils.leftPad("bat", -1) = "bat" // 指定文字を追加する StringUtils.leftPad(null, *, *) = null StringUtils.leftPad("", 3, 'z') = "zzz" StringUtils.leftPad("bat", 3, 'z') = "bat" StringUtils.leftPad("bat", 5, 'z') = "zzbat" StringUtils.leftPad("bat", 1, 'z') = "bat" StringUtils.leftPad("bat", -1, 'z') = "bat"
length
文字列の長さを取得する nullの場合は0を返すStringUtils.length(null) = 0 StringUtils.length("") = 0 StringUtils.length("abcde") = 5
countMatches
指定文字数をカウントするStringUtils.countMatches(null, *) = 0 StringUtils.countMatches("", *) = 0 StringUtils.countMatches("abba", null) = 0 StringUtils.countMatches("abba", "") = 0 StringUtils.countMatches("abba", "a") = 2 StringUtils.countMatches("abba", "ab") = 1 StringUtils.countMatches("abba", "xxx") =
upperCase
文字列を大文字にする nullの場合nullを返すStringUtils.upperCase(null) = null StringUtils.upperCase("") = "" StringUtils.upperCase("aBc") = "ABC"
lowerCase
文字列を小文字にする nullの場合はnullを返すStringUtils.lowerCase(null) = null StringUtils.lowerCase("") = "" StringUtils.lowerCase("aBc") = "abc"
caplitalize
文字列の先頭を大文字にするStringUtils.capitalize(null) = null StringUtils.capitalize("") = "" StringUtils.capitalize("cat") = "Cat" StringUtils.capitalize("cAt") = "CAt"
uncapitalize
文字列の先頭を小文字にするStringUtils.uncapitalize(null) = null StringUtils.uncapitalize("") = "" StringUtils.uncapitalize("Cat") = "cat" StringUtils.uncapitalize("CAT") = "cAT"
swapCase
大文字、小文字を逆転するStringUtils.swapCase(null) = null StringUtils.swapCase("") = "" StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
defaultString
文字列がnullなら空文字で返す// 空文字を返す StringUtils.defaultString(null) = "" StringUtils.defaultString("") = "" StringUtils.defaultString("bat") = "bat" // 指定文字を返す StringUtils.defaultString(null, "NULL") = "NULL" StringUtils.defaultString("", "NULL") = "" StringUtils.defaultString("bat", "NULL") = "bat"
reverse
文字列を逆に並び替えるStringUtils.reverse(null) = null StringUtils.reverse("") = "" StringUtils.reverse("bat") = "tab"
reverseDelimited
特定の文字で区切られている文字列を逆に並び替えるStringUtils.reverseDelimited(null, *) = null StringUtils.reverseDelimited("", *) = "" StringUtils.reverseDelimited("a.b.c", 'x') = "a.b.c" StringUtils.reverseDelimited("a.b.c", ".") = "c.b.a"
abbreviate
文字列を「...」で省略する// 最大文字数指定で、指定文字数-3移行を省略して「...」で表示する StringUtils.abbreviate(null, *) = null StringUtils.abbreviate("", 4) = "" StringUtils.abbreviate("abcdefg", 6) = "abc..." StringUtils.abbreviate("abcdefg", 7) = "abcdefg" StringUtils.abbreviate("abcdefg", 8) = "abcdefg" StringUtils.abbreviate("abcdefg", 4) = "a..." StringUtils.abbreviate("abcdefg", 3) = IllegalArgumentException // 最大文字数指定で、開始位置から省略して「...」で表示する StringUtils.abbreviate(null, *, *) = null StringUtils.abbreviate("", 0, 4) = "" StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 0, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 1, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 4, 10) = "abcdefg..." StringUtils.abbreviate("abcdefghijklmno", 5, 10) = "...fghi..." StringUtils.abbreviate("abcdefghijklmno", 6, 10) = "...ghij..." StringUtils.abbreviate("abcdefghijklmno", 8, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghijklmno", 12, 10) = "...ijklmno" StringUtils.abbreviate("abcdefghij", 0, 3) = IllegalArgumentException StringUtils.abbreviate("abcdefghij", 5, 6) = IllegalArgumentException