jp.littlesoft.util
クラス StringUtil

java.lang.Object
  上位を拡張 jp.littlesoft.util.StringUtil
すべての実装されたインタフェース:
Serializable

public final class StringUtil
extends Object
implements Serializable

Stringに関するユーティリティメソッドを集めたクラス。

バージョン:
1.3.0
作成者:
LittleSoft Corporation
関連項目:
直列化された形式

フィールドの概要
static String COMMA_REPLACE
          コンマを内部的に置換する際の文字列を意味する定数です。
static String CONST_STRING
          replaceConstStringメソッド内でクォート文字で囲まれた文字列を置換する際の プレフィックスを意味する定数です。
 
メソッドの概要
static String arrayToString(boolean[] array)
          boolean型の配列を","で連結します。
static String arrayToString(String[] array)
          String型の配列を","で連結します。
static int countCharacter(char c, String str)
          指定された文字の数を数えます。
static String deleteCR(String str)
          Win改行対応です。
static String deleteCRLF(String str)
          Win改行対応です。
static String deleteFirstLastDblQuote(String str)
          strがダブルクォーテーションで囲まれている場合、それを除去した文字列を返します。
static int findChar(String readData, int startIndex, int endIndex, char ch, boolean passQuote)
          文字列から、指定した一文字の位置を返します。
static int findChars(String readData, int startIndex, int endIndex, char[] chars)
          文字列から、指定した文字の位置を返します。
static int findNotSpace(String readData, int startIndex, int endIndex)
          文字列から、空白以外の文字の位置を返します。
static int findSpace(String readData, int startIndex, int endIndex, boolean passQuote)
          文字列から、空白文字の位置を返します。
static int findString(String readData, int startIndex, int endIndex, String findStr, boolean passQuote)
          文字列から、指定した文字列の位置を返します。
static byte[] getAsciiBytes(String str)
          与えられた文字列をASCII文字としてバイト配列に変換します。
static String getCharset(byte[] datas)
          BOMよりCharsetを読み取ります。
static int getIndex(char[] chars, char c)
          char配列の中の文字のインデックスを返します。
static String insertCR(String str)
          Win改行対応です。
static boolean isEqualString(String str1, String str2)
          2つの文字列が同じかどうかを調べます。
static boolean isEqualStringOrBothNull(String str1, String str2)
          2つの文字列が同じかどうかを調べます。
static boolean isNotNullString(String str)
          文字列が null 以外で、長さ1以上かどうかを調べます。
static boolean isNullString(String str)
          文字列が null または長さ0かどうかを調べます。
static String lpad(String str, char pad, int len)
          指定した長さに満たない場合、指定した文字で左側を埋めた文字列を返します。
static boolean parseBoolean(String str, boolean defaultValue)
          文字列をBoolean値として解析します。
static int parseInt(String str, int defaultValue)
          文字列を int値として解析します。
static String replace(String str, String from, String to)
          文字列をクエリーに適したかたちに変換します。
static String replaceCharToESC(String str)
          String中の文字参照を変換します。
static String replaceCharToESC(String str, boolean cntlToSpc)
          String中の文字参照を変換します。
static String replaceCharToESC(String str, boolean cntlToSpc, boolean lfToCrlf)
          String中の文字参照を変換します。
static String replaceConstString(String parseString, char[] quoteChars, boolean passSpaces, Map hashMap)
          文字列中の quoteCharのいずれかで囲まれた文字列を置換して、HashMapに格納します。
static String replaceConstString(String parseString, char quoteChar, Map hashMap)
          文字列中の quoteCharで囲まれた文字列を置換して、HashMapに格納します。
static String replaceCRLFtoSP(String str)
          Win改行対応です。
static String replaceESCToChar(String str)
          String中の文字参照を変換します。
static String replaceForJavaScript(String str, boolean replaceYen)
          String中の文字をJavaScript用に変換します。
static String trimLeft(String str)
          文字列の左側からだけトリミング処理を行います。
static String trimRight(String str)
          文字列の右側からだけトリミング処理を行います。
static String trimSpace(String str)
          前後の空白や改行文字を除去した文字列を返します。
 
クラス java.lang.Object から継承されたメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

フィールドの詳細

COMMA_REPLACE

public static final String COMMA_REPLACE
コンマを内部的に置換する際の文字列を意味する定数です。

関連項目:
定数フィールド値

CONST_STRING

public static final String CONST_STRING
replaceConstStringメソッド内でクォート文字で囲まれた文字列を置換する際の プレフィックスを意味する定数です。

関連項目:
定数フィールド値
メソッドの詳細

insertCR

public static String insertCR(String str)
Win改行対応です。

\nを見つけたら、その前に\rを挿入します。

パラメータ:
str - \rを挿入前の文字列(!=null)
戻り値:
\rを挿入後の文字列

deleteCR

public static String deleteCR(String str)
Win改行対応です。

CR('\r')を除去した文字列を返します。

パラメータ:
str - \rを含む文字列(!=null)
戻り値:
\rを除去した文字列

deleteCRLF

public static String deleteCRLF(String str)
Win改行対応です。

CR('\r') と LF('\n') を除去した文字列を返します。

パラメータ:
str - \rを含む文字列(!=null)
戻り値:
\rを除去した文字列

replaceCRLFtoSP

public static String replaceCRLFtoSP(String str)
Win改行対応です。

CR('\r') と LF('\n') を半角空白に変換した文字列を返します。

パラメータ:
str - CR,LFを変換したい文字列
戻り値:
CR,LFを変換した文字列

deleteFirstLastDblQuote

public static String deleteFirstLastDblQuote(String str)
strがダブルクォーテーションで囲まれている場合、それを除去した文字列を返します。

パラメータ:
str - 前後のダブルクォーテーションを除去したい文字列
戻り値:
strから前後のダブルクォーテーションを除去した文字列

isNullString

public static final boolean isNullString(String str)
文字列が null または長さ0かどうかを調べます。

パラメータ:
str - nullかどうかを判定したい文字列
戻り値:
True:null(または長さ0)、False:null(または長さ0)ではない

isNotNullString

public static final boolean isNotNullString(String str)
文字列が null 以外で、長さ1以上かどうかを調べます。

パラメータ:
str - nullかどうかを判定したい文字列
戻り値:
True:null(または長さ0)ではない、False:null(または長さ0)

isEqualString

public static final boolean isEqualString(String str1,
                                          String str2)
2つの文字列が同じかどうかを調べます。
str1 または str2 のいずれか、もしくは両方が null の場合は falseを返します。

パラメータ:
str1 - 比較したい文字列
str2 - 比較したい文字列
戻り値:
True:2つの文字列が同じ、False:同じではない(またはどちらか一方または両方がnull)

isEqualStringOrBothNull

public static final boolean isEqualStringOrBothNull(String str1,
                                                    String str2)
2つの文字列が同じかどうかを調べます。

str1, str2 の両方がnullの場合もtrueを返します。

※注 str1=null, str2="" の場合はfalseを返します。

パラメータ:
str1 - 比較したい文字列
str2 - 比較したい文字列
戻り値:
True:2つの文字列が同じ(または両方がnull)、False:同じではない(またはどちらか一方がnull)

trimSpace

public static final String trimSpace(String str)
前後の空白や改行文字を除去した文字列を返します。

先頭の' ', タブ, 改行文字と
末尾の' ', タブ, 改行文字を除去した文字列を返します。

例:
trimSpace(" \t ABC ") ===> "ABC" が結果となる。

パラメータ:
str - 変換したい文字列
戻り値:
変換後の文字列

parseInt

public static final int parseInt(String str,
                                 int defaultValue)
文字列を int値として解析します。

解析できなかった場合は defautValue を返します。

パラメータ:
str - int値として解析したい文字列
戻り値:
解析したint値

parseBoolean

public static final boolean parseBoolean(String str,
                                         boolean defaultValue)
文字列をBoolean値として解析します。

strがnullまたは長さ0の場合は defaultValue を返します。

strが長さ1以上で、"true"以外の場合(大文字、小文字は区別しない)は falseを返します。

パラメータ:
str - boolean値として解析したい文字列
defaultValue - デフォルトのboolean値
戻り値:
解析したboolean値

replaceCharToESC

public static String replaceCharToESC(String str)
String中の文字参照を変換します。

'<' => "&lt;"
'>' =>" &gt;"
'&' => "&amp;"
'¥'' => "&apos;"
'"' => "&quot;"

パラメータ:
str - 変換前文字列
戻り値:
変換後文字列

replaceCharToESC

public static String replaceCharToESC(String str,
                                      boolean cntlToSpc)
String中の文字参照を変換します。

'<' => "&lt;"
'>' => "&gt;"
'&' => "&amp;"
'¥'' => "&apos;"
'"' => "&quot;"

パラメータ:
str - 変換前文字列
cntlToSpc - True:\t,\r,\n以外の0-31までの文字コードを半角空白に置換する、False:置換しない
戻り値:
変換後文字列

replaceCharToESC

public static String replaceCharToESC(String str,
                                      boolean cntlToSpc,
                                      boolean lfToCrlf)
String中の文字参照を変換します。

'<' => "&lt;"
'>' => "&gt;"
'&' => "&amp;"
'¥'' =>" &apos;"
'"' => "&quot;"

パラメータ:
str - 変換前文字列
cntlToSpc - True:\t,\r,\n以外の0-31までの文字コードを半角空白に置換する、False:置換しない
lfToCrlf - '\n' を "\r\n"に変換するか?
戻り値:
変換後文字列

replaceESCToChar

public static String replaceESCToChar(String str)
String中の文字参照を変換します。

"&lt;" => '<'
"&gt;" => '>'
"&nbsp;" => ' '
"&amp;" => '&'
"&apos;" => '¥''
"&quot;" => '¥"'

パラメータ:
str - 変換前文字列
戻り値:
変換後文字列

replaceForJavaScript

public static String replaceForJavaScript(String str,
                                          boolean replaceYen)
String中の文字をJavaScript用に変換します。

"¥'" => "¥¥¥'"
"¥¥" => "¥¥¥¥"

パラメータ:
str - 変換前文字列
replaceYen - True:'¥¥'を"¥¥¥¥"に変換する、False:変換しない
戻り値:
変換後文字列

countCharacter

public static int countCharacter(char c,
                                 String str)
指定された文字の数を数えます。

例: countCharacter('X', "abcXdefX") ==< 2

パラメータ:
c - 個数を数えたい文字
str - 検索対象の文字列
戻り値:
指定された文字の数

getAsciiBytes

public static byte[] getAsciiBytes(String str)
与えられた文字列をASCII文字としてバイト配列に変換します。

str.getBytes("8859_1"); と同じ動作ですが、高速に動作します。

※注1 strにASCII文字以外が含まれていた場合、正しい結果を返しません。
※注2 strのヌルチェックは行っていません。事前にチェックしてください。

パラメータ:
str - バイト配列に変換したい文字列(ヌルは禁止)
戻り値:
strをASCII文字としてバイト配列に変換した結果

replace

public static String replace(String str,
                             String from,
                             String to)
文字列をクエリーに適したかたちに変換します。

パラメータ:
str - 対象文字列
from - 置換対象の部分文字列 (長さ1以上のString)
to - 第2引数を置換する文字列
戻り値:
置換後の文字列

arrayToString

public static String arrayToString(String[] array)
String型の配列を","で連結します。

※注: String中に含まれる","は"&comma"に置換される。

パラメータ:
array - 連結したいStringの配列
戻り値:
連結結果

arrayToString

public static String arrayToString(boolean[] array)
boolean型の配列を","で連結します。

パラメータ:
array - 連結したいStringの配列
戻り値:
連結結果

findChar

public static int findChar(String readData,
                           int startIndex,
                           int endIndex,
                           char ch,
                           boolean passQuote)
文字列から、指定した一文字の位置を返します。

パラメータ:
readData - 検索対象の文字列
startIndex - 検索開始位置
endIndex - 検索終了位置
ch - 検索したい文字
passQuote - True:クォート文字を読み飛ばす、False:読み飛ばさない
戻り値:
検索文字の位置

findChars

public static int findChars(String readData,
                            int startIndex,
                            int endIndex,
                            char[] chars)
文字列から、指定した文字の位置を返します。

charsで指定した文字のいずれかを見つけたら、その位置を返します。

パラメータ:
readData - 検索対象の文字列
startIndex - 検索開始位置
endIndex - 検索終了位置
chars - 検索したい文字の配列
戻り値:
検索文字の位置

findSpace

public static int findSpace(String readData,
                            int startIndex,
                            int endIndex,
                            boolean passQuote)
文字列から、空白文字の位置を返します。

パラメータ:
readData - 検索対象の文字列
startIndex - 検索開始位置
endIndex - 検索終了位置
passQuote - True:クォート文字を読み飛ばす、False:読み飛ばさない
戻り値:
空白文字の位置(見つからない場合は-1)

findNotSpace

public static int findNotSpace(String readData,
                               int startIndex,
                               int endIndex)
文字列から、空白以外の文字の位置を返します。

パラメータ:
readData - 検索対象の文字列
startIndex - 検索開始位置
endIndex - 検索終了位置
戻り値:
空白以外の文字の位置(見つからない場合は-1)

findString

public static int findString(String readData,
                             int startIndex,
                             int endIndex,
                             String findStr,
                             boolean passQuote)
文字列から、指定した文字列の位置を返します。

パラメータ:
readData - 検索対象の文字列
startIndex - 検索開始位置
endIndex - 検索終了位置
findStr - 検索したい文字列
passQuote - True:クォート文字を読み飛ばす、False:読み飛ばさない
戻り値:
検索文字列の位置(見つからない場合は-1)

getIndex

public static int getIndex(char[] chars,
                           char c)
char配列の中の文字のインデックスを返します。

※注 charsの要素数が多い場合には、Arrays.sortおよびArrays.binarySearchを 利用することを検討してください。

パラメータ:
chars - 検索対象の文字配列
c - 検索したい文字
戻り値:
検索文字のインデックス(見つからない場合は-1)

lpad

public static String lpad(String str,
                          char pad,
                          int len)
指定した長さに満たない場合、指定した文字で左側を埋めた文字列を返します。

例: lpad("abc", 'x', 5) ==> "xxabc"

パラメータ:
str - 元の文字列
pad - 追加する文字
len - 文字列の長さ
戻り値:
指定した長さに満たない場合、指定した文字で左側を埋めた文字列

trimLeft

public static String trimLeft(String str)
文字列の左側からだけトリミング処理を行います。

パラメータ:
str - 処理対象文字列
戻り値:
処理結果

trimRight

public static String trimRight(String str)
文字列の右側からだけトリミング処理を行います。

パラメータ:
str - 処理対象文字列
戻り値:
処理結果

getCharset

public static String getCharset(byte[] datas)
BOMよりCharsetを読み取ります。

パラメータ:
datas - 先頭の3バイト
戻り値:
Charset(特定できなかった場合は、"8859_1"

replaceConstString

public static String replaceConstString(String parseString,
                                        char quoteChar,
                                        Map hashMap)
文字列中の quoteCharで囲まれた文字列を置換して、HashMapに格納します。

同時に余計な空白を除去している。
また、\"など\に続く文字の変換も行っている。
例:
   String result = StringUtil.replaceConstString("\"XXX\"+\"AAA\\\"BBB\"+\"CCC\"", '\"', map);
   --> result = "_CONSTSTR0_+_CONSTSTR1_+_CONSTSTR2_" となる。
          mapには、key=_CONSTSTR0_, value="XXX"
                   key=_CONSTSTR1_, value="AAA\"BBB"
                   key=_CONSTSTR2_, value="CCC"
          が格納される。
 

パラメータ:
parseString - 変換前の文字列
quoteChar - QuoteChar
hashMap - 置換した内容を格納するMap(key="_CONSTSTRn_"(n=0,1,2,...), value=元の文字列)
戻り値:
変換後の文字列

replaceConstString

public static String replaceConstString(String parseString,
                                        char[] quoteChars,
                                        boolean passSpaces,
                                        Map hashMap)
文字列中の quoteCharのいずれかで囲まれた文字列を置換して、HashMapに格納します。

同時に余計な空白を除去している。
また、\"など\に続く文字の変換も行っている。
例:
   String result = StringUtil.replaceConstString("\"XXX\"+'AAA\\\"BBB'+\"CCC\"", new char[]{'\"', '\''}, map);
   --> result = "_CONSTSTR0_+_CONSTSTR1_+_CONSTSTR2_" となる。
          mapには、key=_CONSTSTR0_, value="XXX"
                   key=_CONSTSTR1_, value="AAA\"BBB"
                   key=_CONSTSTR2_, value="CCC"
          が格納される。
 

パラメータ:
parseString - 変換前の文字列
quoteChars - QuoteCharの配列
passSpaces - True:空白文字(' ','\r','\n','\t'を読み飛ばす、False:読み飛ばさない
hashMap - 置換した内容を格納するMap(key="_CONSTSTRn_"(n=0,1,2,...), value=元の文字列)
戻り値:
変換後の文字列


Copyright (C) 2006-2012 LittleSoft Corporation. All Rights Reserved.