Bash文字列処理(Javaと照合)-19.文字の位置を検索


Bash文字列処理(Javaと照合)-19.文字の位置を検索
In Java
String.indexOf & String.lastIndexOf
 int     indexOf(int ch)           指定した文字列がこの文字列に最初に表示されたインデックスを返します. int     indexOf(int ch, int fromIndex)           指定したインデックスから検索を開始し、この文字列に指定した文字が最初に表示されたインデックスを返します.
 
int     lastIndexOf(int ch)           最後に表示された指定文字列のインデックスを返します. int     lastIndexOf(int ch, int fromIndex)           指定したインデックスから後方検索を開始し、最後に表示された指定文字列のインデックスを返します.
 
StringUtils.indexOf & StringUtils.indexOfAny & StringUtils.indexOfIgnoreCase & StringUtils.lastIndexOf
org.apache.commons.lang.StringUtilsでは、順方向および逆方向を含む多くの文字インデックスを検索する方法が提供されています.
 
static int     indexOf(String str, char searchChar)           Finds the first index within a String, handling null. static int     indexOf(String str, char searchChar, int startPos)           Finds the first index within a String from a start position, handling null.
static int     lastIndexOf(String str, char searchChar)           Finds the last index within a String, handling null. static int     lastIndexOf(String str, char searchChar, int startPos)           Finds the last index within a String from a start position, handling null.
 
org.apache.commons.lang.StringUtilsでは、任意の文字が表示される場所を検索する方法も提供されています.static int     indexOfAny(String str, char[] searchChars)           Search a String to find the first index of any character in the given set of characters. static int     indexOfAny(String str, String searchChars)           Search a String to find the first index of any character in the given set of characters. static int     indexOfAnyBut(String str, char[] searchChars)           Search a String to find the first index of any character not in the given set of characters. static int     indexOfAnyBut(String str, String searchChars)           Search a String to find the first index of any character not in the given set of characters.
 
In Bash
文字の位置を検索するには、文字を巡回する方法を使用します.
関数:strchr
見つかった場合、印刷文字の位置は0からカウントされ、終了コードは0になります.そうでなければ-1を印刷し、終了コードは1です.
strchr(){
    local i
    for ((i=0; i<${#1}; ++i))
    do
        if [ "${1:i:1}" == "$2" ]; then
            echo $i
            return 0
        fi
    done
    echo -1
    return 1
}

 
[root@web ~]# STR=123456789 [root@web ~]# CH=6 [root@web ~]# strchr "$STR""$CH" 5 [root@web ~]# echo $? 0 [root@web ~]# CH=a [root@web ~]# strchr "$STR""$CH" -1 [root@web ~]# echo $? 1 [root@web ~]#
 
expr indexで文字の位置を検索する
形式:expr index"$STR"$CHARS"
STRでは、サブストリングではなくCHARSの任意の文字を検索し、最初の位置を印刷します.
注意:返される下付き文字は1から始まり、0は見つからないことを示します.
JavaのindexOfメソッドに完全に対応していません.C++STL stringとのfind_first_of似ています.
 
man exprは
index STRING CHARS
   index in STRING where any CHARS is found, or 0
 
[root@jfht ~]# STR="Hello World" [root@jfht ~]# SUB="l" [root@jfht ~]# expr index "$STR""$SUB" 3
[root@jfht ~]# SUB="not found"     # なお、expr indexは、サブストリングを検索できる位置ではなく、文字列内の任意の文字が初めて現れる位置である[root@jfht ~]# expr index "$STR""$SUB" 5
 
awk indexで文字の出現位置を検索する
フォーマット1:awk-v"STR=$STR"-v"CH=$CH"'{print index(STR,CH)}'<""
フォーマット2:echo|awk-v"STR=$STR"-v"CH=$CH"'{print index(STR,CH)}'
awkはデフォルトで標準入力からデータを読み出すので、入力のリダイレクトが必要です.
この方法では、文字の出現位置だけでなく、サブストリングの出現位置もクエリーできます.ただし、任意の文字の出現位置は検索できません.
注意:インデックスの位置は1からカウントされ、0は見つかりません.
manawkは
index(s, t) Returns the index of the string t in the string s, or 0 if t is not present. (This
implies that character indices start at one.)
 
[root@web ~]# STR=123456789 [root@web ~]# CH=6
[root@web ~]# awk -v "STR=$STR"-v "CH=$CH"'{print index(STR,CH)}' <<<"" 6 [root@web ~]# echo | awk -v "STR=$STR"-v "CH=$CH"'{print index(STR,CH)}' 6 [root@web ~]#
 
 
本文リンク:http://codingstandards.iteye.com/blog/1198917 (転載は出典を明記してください)
リターンディレクトリ:JavaプログラマーのBashユーティリティーガイドシリーズの文字列処理(ディレクトリ) 
前節内容:Bash文字列処理(Javaと照合)-18.文字列のフォーマット
下節内容:Bash文字列処理(Javaと照合)-20.サブストリングの場所を検索