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

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 ~]#


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/406593.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mint-ui 写一个下拉滑动选择,mt-popup和mt-picker结合使用

<template><div id"feedback"><div click"getpopupVisible">产品选择</div><mt-popupv-model"popupVisible"popup-transition"popup-fade"position"bottom"><div class"picker-toolb…

够学习一辈子的生活经典

说话要用脑子&#xff0c;敏事慎言&#xff0c;话多无益&#xff0c;嘴只是一件扬声器而已&#xff0c;平时一定要注意监督、控制好调频旋钮和音控开关&#xff0c;否则会给自己带来许多麻烦。讲话不要只顾一时痛快、信口开河&#xff0c;以为人家给你笑脸就是欣赏&#xff0c;…

设计模式之单一职责原则

超前的设计或者过度的设计都不是良好的设计&#xff0c;很多时候我们等到代码在第一次变化的时候可以及时作出反应。 What 就一个类&#xff08;接口、结构体、方法等等&#xff09;而言&#xff0c;应该仅有一个引起它变化的原因。 Why 软件设计真正要做的许多内容&#xff0c…

makefile常用语法讲解(1)

From: http://www.cnblogs.com/mydomain/archive/2011/08/12/2136083.html 1、make是一个解释makefile中指令的命令工具。Make工具最主要也是最基本的功能就是通过makefile文件来描述源程序之间的相互关系并自动维护编译工作。而makefile 文件需要按照某种语法进行编写&#…

HTML5如何控制暂停播放停止

本篇教程探讨了HTML5如何控制暂停播放停止&#xff0c;希望阅读本篇文章以后大家有所收获&#xff0c;帮助大家HTML5CSS3从入门到精通 。 <!DOCTYPE HTML> <html> <head> <meta charset"utf-8"> <meta name"viewport" content…

Oracle 高水位(HWM: High Water Mark) 说明

一. 准备知识&#xff1a;ORACLE的逻辑存储管理. ORACLE在逻辑存储上分4个粒度: 表空间, 段, 区 和 块. 1.1 块: 是粒度最小的存储单位,现在标准的块大小是8K,ORACLE每一次I/O操作也是按块来操作的,也就是说当ORACLE从数据文件读数据时,是读取多少个块,而不是多少行. 每一个B…

React开发(153):ant design自定义列

<Row gutter{12}><Col span{12}><Form.Item label"省/市/区"><CascaderfieldNames{fieldNames}options{options}onChange{() > {this.onChange();}}placeholder"请输入"/></Form.Item></Col>

[转]那些年我还不懂:IList,ICollection,IEnumerable,IEnumerator,IQueryable

1、首先看一个简单的例子 int[] myArray { 1, 32, 43, 343 };IEnumerator myie myArray.GetEnumerator();myie.Reset();while (myie.MoveNext()){int i (int)myie.Current;Console.WriteLine("Value: {0}", i);} 相信很多人都不会像上面这样去遍历myArray这个数组…

makefile常用讲解(2)

From: http://www.cnblogs.com/mydomain/archive/2011/08/12/2136085.html 4&#xff09;变量的引入 变量的引入和应用&#xff1a; CCgcc HD-I headers SC-c $< OBJ-o $ bin/st_work : obj/main.o obj/st_work.o obj/fun.o gcc $^ -o $ (命令一定要用以Tab…

router-link

组件的属性有&#xff1a; to 、replace、 append、 tag、 active-class、 exact 、 event、 exact-active-class to&#xff08;必选参数&#xff09;&#xff1a;类型string/location 表示目标路由的链接&#xff0c;该值可以是一个字符串&#xff0c;也可以是动态绑定的描…

Qt编程'hello world

#include<QApplication>#include<QLabel>int main(int argc,char*argv[]){QApplicatin app(argc,argv);QLabel *label("hello");label->show();return app.exec()} 编译运行&#xff1a; qmake -project 生成 .pro文件qmake 生成 makemake执行 转载于…

电子工程师必上的十大专业网站

这是很久以前看过的帖子&#xff0c;感觉非常不错&#xff0c;今天特地找出来&#xff0c;便于自己以后经常翻阅&#xff01;&#xff01;今天再看这篇文章&#xff0c;发现很多网站自己还是没有账号&#xff0c;原来自己还是非常的不进取啊。来源&#xff1a;hi.baidu.com 作者…

Makefile文件编写规则

From: http://aviva.iteye.com/blog/807494 Makefile中包含五种内容&#xff1a;显式规则&#xff0c;隐式规则&#xff0c;变量定义&#xff0c;指令&#xff08;directive&#xff09;和注释。 1.显式规则――描述如何生成规则的目标&#xff0c;它列出了目标依赖的文件&…

vue使用better-scroll实现下拉刷新、上拉加载

本文目的是为了实现列表的下拉刷新、上拉加载&#xff0c;所以选择了better-scroll这个库。 用好这个库&#xff0c;需要理解下面说明 必须包含两个大的div&#xff0c;外层和内层div 外层div设置可视的大小(宽或者高)-有限制宽或高 内层div&#xff0c;包裹整个可以滚动的部分…

OpenCV学习 4:摄像头视频读写与边缘检测

原创文章&#xff0c;欢迎转载&#xff0c;转载请注明出处 想实现人脸识别&#xff0c;车辆识别&#xff0c;车牌识别。一般我们可不是读硬盘里面的视频文件的&#xff0c;都是直接从摄像头读取视频流然后直接识别的。所以读取摄像头的视频流这是基础。。。OpenCV对读取摄像头的…

Linux中自带正则表达式应用举例

环境&#xff1a;Fedora12, C程序&#xff1a; #include <stdio.h> #include <string.h> #include <sys/types.h> #include <regex.h>// 提取子串 char* getsubstr(char *s, regmatch_t *pmatch) {static char buf[100] {0};memset(buf, 0, sizeof(b…

ISAPI_Rewrite 规则说明

I (ignore case&#xff09;不管大小写强行指定字符匹配例&#xff1a;RewriteRule /code/project/([0-9,a-z]*).html /soft.jsp\?softpy$1 [I]其他的参数一览I (ignore case&#xff09;不管大小写强行指定字符匹配&#xff0c;这个FLAG影响RewriteRule指令和相应的RewriteCo…

H5页面唤起指定app或跳转到应用市场

场景1&#xff1a; 在 h5 页面上&#xff0c;不管用户是否安装过该app&#xff0c;都直接跳转到应用市场&#xff0c;让用户从应用市场上打开app。 思路&#xff1a; 这种场景处理比较简单&#xff0c;直接判断判断是android端还是ios端&#xff0c;然后在点击按钮上赋值对应…