为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

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

2017-12-10 5页 doc 19KB 17阅读

用户头像

is_314871

暂无简介

举报
Bash字符串处理(与Java对照) - 20_查找子串的位置Bash字符串处理(与Java对照) - 20_查找子串的位置 Bash字符串处理(与Java对照) - 20.查找子串的位置 In Java String.indexOf int indexOf(String str) 返回第一次出现的指定子字符串在此字符串中的索引。 int indexOf(String str, int fromIndex) 从指定的索引处开始,返回第一次出现的指定子字符串在此字符串 中的索引。 int lastIndexOf(String str) 返回在此字符串中最右边出现的指定子...
Bash字符串处理(与Java对照) - 20_查找子串的位置
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 如果找到,打印位置,从0开始计数,退出码为0;否则,打印-1,退出码为1 strstr(){ declare -i i n2=${#2} n1=${#1}-n2 #echo $i $n1 $n2 for ((i=0; i
示没有找到。 格式: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 本文链接:file:///D:/blog/1199992 (转载请注明出处) 返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 上节内容:Bash字符串处理(与Java对照) - 19.查找字符的位置 下节内容:Bash字符串处理(与Java对照) - 21.字符串(正则)匹配
/
本文档为【Bash字符串处理(与Java对照) - 20_查找子串的位置】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索