天天看點

Bash字元串處理(與Java對照) - 8.計算字元串長度Bash字元串處理(與Java對照) - 8.計算字元串長度

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}

[[email protected] ~]# STR="Hello World"

[[email protected] ~]# 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

[[email protected] ~]# STR="Hello World"

[[email protected] ~]# expr length $STR

expr: 文法錯誤

因為STR中包含空白,造成了問題,要加上雙引号。

[[email protected] ~]# expr length "$STR"

11

[[email protected] ~]# expr "$STR" : ".*"

11

[[email protected] ~]# 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  

[[email protected] ~]# STR=123456789

[[email protected] ~]# echo ${#STR}

9

[[email protected] ~]# wc -c <<<"$STR "

10

[[email protected] ~]# echo -n "$STR" | wc -c

9

[[email protected] ~]# wc -m <<<"$STR"

10

[[email protected] ~]# echo -n "$STR" | wc -m

9

[[email protected] ~]# STR=今年是2011年

[[email protected] ~]# echo ${#STR}

8

[[email protected] ~]# wc -c <<<"$STR" 

13

[[email protected] ~]# echo -n "$STR" | wc -c

12

[[email protected] ~]# wc -m <<<"$STR"

9

[[email protected] ~]# echo -n "$STR" | wc -m

8

[[email protected] ~]# STR="Hello World"

[[email protected] ~]# echo ${#STR}

11

[[email protected] ~]# wc -c <<<"$STR"

12

[[email protected] ~]# echo -n "$STR" | wc -c

11

[[email protected] ~]# wc -m <<<"$STR"

12

[[email protected] ~]# echo -n "$STR" | wc -m

11

本文連結:http://codingstandards.iteye.com/blog/1173125   (轉載請注明出處)

傳回目錄:Java程式員的Bash實用指南系列之字元串處理(目錄) 

上節内容:Bash字元串處理(與Java對照) - 7.字元串與預設值

下節内容:Bash字元串處理(與Java對照) - 9.擷取字元串指定位置的字元、周遊字元串中的字元