`

Bash字符串处理(与Java对照) - 20.查找子串的位置

阅读更多

Bash字符串处理(与Java对照) - 20.查找子串的位置

In Java

String.indexOf

 int     indexOf(String str)
          返回第一次出现的指定子字符串在此字符串中的索引。
 int     indexOf(String str, int fromIndex)
          从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。

 

 int     lastIndexOf(String str)
          返回在此字符串中最右边出现的指定子字符串的索引。
 int     lastIndexOf(String str, int fromIndex)
          从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。

 

StringUtils.indexOf & StringUtils.lastIndexOf

static int     indexOf(String str, String searchStr)
          Finds the first index within a String, handling null.
static int     indexOf(String str, String searchStr, int startPos)
          Finds the first index within a String, handling null.
static int     indexOfAny(String str, String[] searchStrs)
          Find the first index of any of a set of potential substrings.
static int     indexOfIgnoreCase(String str, String searchStr)
          Case in-sensitive find of the first index within a String.
static int     indexOfIgnoreCase(String str, String searchStr, int startPos)
          Case in-sensitive find of the first index within a String from the specified position.
static int     lastIndexOf(String str, String searchStr)
          Finds the last index within a String, handling null.
static int     lastIndexOf(String str, String searchStr, int startPos)
          Finds the first index within a String, handling null.
static int     lastIndexOfAny(String str, String[] searchStrs)
          Find the latest index of any of a set of potential substrings.
static int     lastIndexOfIgnoreCase(String str, String searchStr)
          Case in-sensitive find of the last index within a String.
static int     lastIndexOfIgnoreCase(String str, String searchStr, int startPos)
          Case in-sensitive find of the last index within a String from the specified position.
static int     lastOrdinalIndexOf(String str, String searchStr, int ordinal)
          Finds the n-th last index within a String, handling null.
static int     ordinalIndexOf(String str, String searchStr, int ordinal)
          Finds the n-th index within a String, handling null.

 

In Bash

使用遍历字符串的方法来查找子串的位置

函数:strstr <str> <sub>

如果找到,打印位置,从0开始计数,退出码为0;否则,打印-1,退出码为1

strstr(){
   declare -i i n2=${#2} n1=${#1}-n2
   #echo $i $n1 $n2
   for ((i=0; i<n1; ++i)){
      if [ "${1:i:n2}" == "$2" ]; then
         echo $i
         return 0
      fi
   }
   echo -1
   return 1
}

 

[root@web ~]# STR=123456789
[root@web ~]# SUB=567
[root@web ~]# strstr "$STR" "$SUB"
4
[root@web ~]#

 

使用awk index来查找子串的位置

索引位置从1开始计数,0表示没有找到。

格式:awk -v "STR=$STR" -v "SUB=$SUB" '{print index(STR,SUB)}' <<<""

格式:echo | awk -v "STR=$STR" -v "SUB=$SUB" '{print index(STR,SUB)}'

 

[root@web ~]# STR=123456789
[root@web ~]# SUB=456
[root@web ~]# awk -v "STR=$STR" -v "SUB=$SUB" '{print index(STR,SUB)}' <<<""
4
[root@web ~]# echo | awk -v "STR=$STR" -v "SUB=$SUB" '{print index(STR,SUB)}'
4
[root@web ~]#

 

使用expr match来查找子串的最后出现位置

注意:expr index不能用来查找子串的位置。

格式1:expr "$STR" : ".*$SUB" - length "$SUB"

格式2:expr match "$STR" ".*$SUB" - length "$SUB"

如果STR包含SUB串,返回最后一次出现的位置(从1开始算)。因为正则表达式采用贪婪匹配方式,所以相当于lastIndexOf。如果没有找到,打印<=0的值。

 

[root@jfht ~]# STR="Hello World"
[root@jfht ~]# SUB=or
[root@jfht ~]# expr "$STR" : ".*$SUB"
9
[root@jfht ~]# expr "$STR" : ".*$SUB" - length "$SUB"
7
[root@jfht ~]# expr match "$STR" ".*$SUB" - length "$SUB" 
7

[root@jfht ~]# SUB=xx
[root@jfht ~]# expr "$STR" : ".*$SUB" - length "$SUB"
-2
[root@jfht ~]# expr match "$STR" ".*$SUB" - length "$SUB"
-2

[root@jfht ~]# SUB=o
[root@jfht ~]# expr "$STR" : ".*$SUB" - length "$SUB"   
7
[root@jfht ~]# expr match "$STR" ".*$SUB" - length "$SUB"
7

 

 

本文链接:http://codingstandards.iteye.com/blog/1199992   (转载请注明出处)

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 

上节内容:Bash字符串处理(与Java对照) - 19.查找字符的位置

下节内容:Bash字符串处理(与Java对照) - 21.字符串(正则)匹配

 

 

 

4
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics