Bash字符串处理(与Java对照) - 15.计算子串出现的次数
In Java
StringUtils.countMatches
org.apache.commons.lang.StringUtils countMatches方法 写道
public static int countMatches(String str, String sub)
Counts how many times the substring appears in the larger String.
A null or empty ("") String input returns 0.
StringUtils.countMatches(null, *) = 0
StringUtils.countMatches("", *) = 0
StringUtils.countMatches("abba", null) = 0
StringUtils.countMatches("abba", "") = 0
StringUtils.countMatches("abba", "a") = 2
StringUtils.countMatches("abba", "ab") = 1
StringUtils.countMatches("abba", "xxx") = 0
Parameters:
str - the String to check, may be null
sub - the substring to count, may be null
Returns:
the number of occurrences, 0 if either String is null
countMatches的实现代码
(摘自 StringUtils)
public static int countMatches(String str, String sub) {
if (isEmpty(str) || isEmpty(sub)) {
return 0;
}
int count = 0;
int idx = 0;
while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) {
count++;
idx += sub.length();
}
return count;
}
public static final int INDEX_NOT_FOUND = -1;
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
In Bash
子串出现的次数
格式:grep -o "$SUB" <<<"$STR" | wc -l
错误:grep -c -o "$SUB" <<<"$STR" 注:开始以为这个可以,经过检验之后不行,因为-c参数只会打印匹配的的行数。
man grep 写道
-o, --only-matching
Show only the part of a matching line that matches PATTERN.
-c, --count
Suppress normal output; instead print a count of matching lines for each input file. With the -v,
--invert-match option (see below), count non-matching lines.
[root@jfht ~]# x="This is a test"
[root@jfht ~]# grep -o "t" <<<"$x" | wc -l
2
[root@jfht ~]# grep -o "T" <<<"$x" | wc -l
1
[root@jfht ~]# grep -o "[tT]" <<<"$x" | wc -l
3
[root@jfht ~]# grep -o "t|T" <<<"$x" | wc -l
0
[root@jfht ~]# grep -o "t\|T" <<<"$x" | wc -l
3
字符出现的次数
格式:TMP=${STR//[^cC]}; ${#TMP}
意思是说将STR字符串中所有不是c和C的字符删除掉,保存到TMP中,那么得到TMP的长度就是c或C出现的次数
[root@jfht ~]# x="This is a test"
[root@jfht ~]# y=${x//[^tT]}
[root@jfht ~]# echo $y
Ttt
[root@jfht ~]# echo ${#y}
3
本文链接:http://codingstandards.iteye.com/blog/1182146
(转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)
上节内容:Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
下节内容:Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头
分享到:
相关推荐
bash-3.1-MSYS-1.0.11-snapshot.tar.bz2 ffmpeg 安装用包!
在centos6或rhel6 操作系统安装bash-completion-2.1-6.el7.noarch.rpm后,重启即生效
网上有很多bash补丁下载,但都是x86_64架构的,针对i686的下载很少,bash-4.1.2-15.el6_5.2.src.rpm 也很难下载,希望上传的这个bash-4.1.2-15.el6_5.2.i686.rpm对你有用。
修复redhat6 bash远程执行任意代码漏洞CVE-2014-6271
离线安装包,测试可用
执行如下命令查看设备是否存在bash安全漏洞。 #env -i X='() { (a)=>\\' bash -c '/dev/stdout echo vulnerable' 2>/dev/null 判定依据 如果以上命令的输出为以下内容则不合规: vulnerable
libguestfs-bash-completion-1.40.2-28.module_el8.5.0+821+97472045.noarch(1).rpm官方离线安装包,亲测可用
-bash: /usr/local/jdk/jdk1.8.0_181/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory 安装完后 java -version 查看版本出现: 原因是:没有那个文件或目录,找了很久发现需要...
wget-1.14-15.el7.x86_64.rpm 解决 -bash:wget command not found
openjdk-18.0.1.1(openjdk-18.0.1.1_linux-x64_bin.tar.gz)适用于Linux x64 系统:
redhat bash漏洞补丁包含文件:bash-4.1.2-15.el6_5.2.i686.rpmbash-4.1.2-15.el6_5.2.x86_64.rpmbash-3.0-27.el4.i386.rpmbash-3.0-27.el4.x86_64.rpmbash-3.2-33.el5_11.4.i386.r... redhat bash漏洞补丁 包含文件...
git-bash.exe
bash-4.1.2-9.el6_2.i686.rpm是centos工具类。
bash-4.1.2-15.el6_5.2.i686.rpm bash-4.1.2-15.el6_4.src.rpm bash-4.1.2-15.el6_5.2.src.rpm 8、rhel6.x_64 bash-4.1.2-15.el6_5.2.x86_64.rpm bash-debuginfo-4.1.2-15.el6_5.2.x86_64.rpm bash-doc-4.1.2-15.el6...
修改redhat 7 bash远程执行任意代码漏洞
在Linux下使用sqlplus或者rman时,经常需要调用上次或之前运行过的命令 下载包地址: ... 地址2: ... 1. rpm -ivh rlwrap-0.42-1.el7.x86_64.rpm ...2. 检查包是否安装上 rpm -qa rlwrap ...4. 生效: source .bash_profile
bash-4.1.2-15.el6_5.2.x86_64
#tar zxvf bash-4.4.tar.gz #cd bash-4.4 #./configure (如果centos7编译失败,请先安装#yum install gcc) #make #make install 添加环境变量 #echo‘ export PATH=/usr/local/bin:$PATH‘ >>/etc/profile #source...
bash安全漏洞修复,记得在 使用命令的时候用 RPM -Uvh