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

Bash代码   收藏代码
  1. strstr(){  
  2.    declare -i i n2=${#2} n1=${#1}-n2  
  3.    #echo $i $n1 $n2  
  4.    for ((i=0; i<n1; ++i)){  
  5.       if [ "${1:i:n2}" == "$2" ]; then  
  6.          echo $i  
  7.          return 0  
  8.       fi  
  9.    }  
  10.    echo -1  
  11.    return 1  
  12. }  

 

[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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值