Bash字符串处理(与Java对照) - 8.计算字符串长度
In Java
取字符数量
一个汉字算1个字符。
int len = s.length();
JavaDoc class String 写道
public int length()
Returns the length of this string. The length is equal to the number of Unicode code units in the string.
Specified by:
length in interface CharSequence
Returns:
the length of the sequence of characters represented by this object.
取字节数量
一个汉字算几个字节,取决于编码方式。
int numOfBytes = s.getBytes().length;
JavaDoc class String 写道
byte[] getBytes()
Encodes this String into a sequence of bytes using the
platform's default charset, storing the result into a new byte array.
byte[] getBytes(Charset charset)
Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
byte[] getBytes(String charsetName)
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
In Bash
取变量STR的长度(推荐方式)
格式:${#STR}
[root@jfht ~]# STR="Hello World"
[root@jfht ~]# echo ${#STR}
11
使用expr length命令取字符串长度
用expr命令,也可以取到字符串长度,但都没有上面的高效,因为上面的方式是Bash内置的方式,而expr命令是外部命令。
格式:expr length $STR
man expr 写道
length STRING
length of STRING
使用expr match命令取字符串长度
格式1:expr "$STR" : ".*"
格式2:expr match "$STR" ".*"
man expr 写道
STRING : REGEXP
anchored pattern match of REGEXP in STRING
match STRING REGEXP
same as STRING : REGEXP
[root@jfht ~]# STR="Hello World"
[root@jfht ~]# expr length $STR
expr: 语法错误
因为STR中包含空白,造成了问题,要加上双引号。
[root@jfht ~]# expr length "$STR"
11
[root@jfht ~]# expr "$STR" : ".*"
11
[root@jfht ~]# expr match "$STR" ".*"
11
用wc命令取字符串长度
使用wc命令也可以实现字符串长度计算。
格式1:wc -c <<<"$STR"
比实际的字节数多1,会多输出一个换行,等同于 echo "$STR" | wc -c 而不是下面这个
格式2:echo -n "$STR" | wc -c
上面是计算字节数,如果是中文的话,每个中文为2个字节(当LANG=zh_CN.GB18030)。
格式3:wc -m <<<"$STR"
比实际的字符数多1,会多输出一个换行,等同于 echo "$STR" | wc -m 而不是下面这个
格式4:echo -n "$STR" | wc -m
上面是计算字符数,与${#STR}相同,每个汉字是按1个字符计算。
man wc 写道
-c, --bytes
print the byte counts
-m, --chars
print the character counts
[root@jfht ~]# STR=123456789
[root@jfht ~]# echo ${#STR}
9
[root@jfht ~]# wc -c <<<"$STR
"
10
[root@jfht ~]# echo -n "$STR" | wc -c
9
[root@jfht ~]# wc -m <<<"$STR"
10
[root@jfht ~]# echo -n "$STR" | wc -m
9
[root@jfht ~]# STR=今年是2011年
[root@jfht ~]# echo ${#STR}
8
[root@jfht ~]# wc -c <<<"$STR"
13
[root@jfht ~]# echo -n "$STR" | wc -c
12
[root@jfht ~]# wc -m <<<"$STR"
9
[root@jfht ~]# echo -n "$STR" | wc -m
8
[root@jfht ~]# STR="Hello World"
[root@jfht ~]# echo ${#STR}
11
[root@jfht ~]# wc -c <<<"$STR"
12
[root@jfht ~]# echo -n "$STR" | wc -c
11
[root@jfht ~]# wc -m <<<"$STR"
12
[root@jfht ~]# echo -n "$STR" | wc -m
11
、
本文链接:http://codingstandards.iteye.com/blog/1173125
(转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)
上节内容:Bash字符串处理(与Java对照) - 7.字符串与默认值
下节内容:Bash字符串处理(与Java对照) - 9.获取字符串指定位置的字符、遍历字符串中的字符
分享到:
相关推荐
bash-3.1-MSYS-1.0.11-snapshot.tar.bz2 ffmpeg 安装用包!
在centos6或rhel6 操作系统安装bash-completion-2.1-6.el7.noarch.rpm后,重启即生效
离线安装包,测试可用
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 查看版本出现: 原因是:没有那个文件或目录,找了很久发现需要...
openjdk-18.0.1.1(openjdk-18.0.1.1_linux-x64_bin.tar.gz)适用于Linux x64 系统:
git-bash.exe
修改redhat 7 bash远程执行任意代码漏洞
bash-4.1.2-9.el6_2.i686.rpm是centos工具类。
在Linux下使用sqlplus或者rman时,经常需要调用上次或之前运行过的命令 下载包地址: ... 地址2: ... 1. rpm -ivh rlwrap-0.42-1.el7.x86_64.rpm ...2. 检查包是否安装上 rpm -qa rlwrap ...4. 生效: source .bash_profile
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漏洞补丁 包含文件...
修复redhat6 bash远程执行任意代码漏洞CVE-2014-6271
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
命令补全 Centos6/7或Rhel6/7 安装 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对你有用。
#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安全漏洞。 #env -i X='() { (a)=>\\' bash -c '/dev/stdout echo vulnerable' 2>/dev/null 判定依据 如果以上命令的输出为以下内容则不合规: vulnerable
.git-completion.bash