Bash字符串处理(与Java对照) - 8.计算字符串长度

本文深入探讨了Bash与Java在计算字符串长度时的不同方法,包括使用Bash内置函数、expr命令、wc命令以及Java的length()方法。通过对比,展示了如何在Bash中高效地获取字符串长度,并提供了相应的实例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.获取字符串指定位置的字符、遍历字符串中的字符

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值