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

来源:互联网 发布:知乎账号 编辑:程序博客网 时间:2024/04/24 00:03

From: http://codingstandards.iteye.com/blog/1198917

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 <str> <ch>

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

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

 

[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表示没有找到。

man awk 写道
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 ~]#


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我母亲怀孕了我的孩子小说 母亲满足了我的要求 子与母床上大战小说 穷山村的娘和儿子全文阅读 乱辈真实故事福林 为什么我没有朋友 妈毛又多又黑 脱掉母亲的白衬衫 漫画 母亲臀部抬起配合我 跟儿子洗澡没忍住做了一次 偷摸熟睡的母H 母亲只好认命抬起右腿 晚上故意到儿子房间睡 小说母亲的短裤衩 抱着母亲的腰疯狂地耸小说 姑妈穿裙子坐在我的腿的故事上 儿子18妈 妈38片国中 顶开高贵母亲的两辨 第章母亲抬腰迎合 母亲开始抗拒慢慢变迎合我口诉 母亲开始抗拒慢慢迎合我 第章顶开高贵母亲两瓣 母亲从抗拒慢慢变的迎合我 母亲疯狂迎合我txt小说下载 来吧儿子妈要你 影视片 儿子进来今晚妈让你做个够电影 儿子今晚妈让你做人个够知音网 离婚后和儿子睡觉 儿子让妈给你留个种吧 儿子妈要你的大吉吧 没开灯把小偷当老公 女儿怀孕我给姑爷解决问题 有给儿子口的吗 儿子你的真大 儿子想怎么弄就怎么弄吧 坏儿子还能在深点吗 睡着儿子顶了进来 儿子别急妈是你的小说 儿子今晚妈让你做人个够的视频 母亲和我做完跟父亲做在线阅读 高粱地里野坑头