bash中的字符串长度

本文翻译自:Length of string in bash

How do you get the length of a string stored in a variable and assign that to another variable? 如何获取存储在变量中的字符串的长度并将其分配给另一个变量?

myvar="some string"
echo ${#myvar}  
# 11

How do you set another variable to the output 11 ? 如何为输出11设置另一个变量?


#1楼

参考:https://stackoom.com/question/1AsE7/bash中的字符串长度


#2楼

To get the length of a string stored in a variable, say: 要获取存储在变量中的字符串的长度,请说:

myvar="some string"
size=${#myvar} 

To confirm it was properly saved, echo it: 要确认已正确保存,请echo它:

$ echo "$size"
11

#3楼

If you want to use this with command line or function arguments, make sure you use size=${#1} instead of size=${#$1} . 如果要在命令行或函数参数中使用它,请确保使用size=${#1}而不是size=${#$1} The second one may be more instinctual but is incorrect syntax. 第二个可能更本能,但语法不正确。


#4楼

You can use: 您可以使用:

MYSTRING="abc123"
MYLENGTH=$(printf "%s" "$MYSTRING" | wc -c)
  • wc -c or wc --bytes for byte counts = Unicode characters are counted with 2, 3 or more bytes. wc -cwc --bytes用于字节计数= Unicode字符以2、3或更多字节计。
  • wc -m or wc --chars for character counts = Unicode characters are counted single until they use more bytes. wc -mwc --chars用于字符计数= Unicode字符wc --chars次计数,直到它们使用更多字节为止。

#5楼

UTF-8 string length UTF-8字符串长度

In addition to fedorqui's correct answer , I would like to show the difference between string length and byte length: 除了fedorqui的正确答案之外 ,我还要显示字符串长度和字节长度之间的区别:

myvar='Généralités'
chrlen=${#myvar}
oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
bytlen=${#myvar}
LANG=$oLang LC_ALL=$oLcAll
printf "%s is %d char len, but %d bytes len.\n" "${myvar}" $chrlen $bytlen

will render: 将呈现:

Généralités is 11 char len, but 14 bytes len.

you could even have a look at stored chars: 您甚至可以查看存储的字符:

myvar='Généralités'
chrlen=${#myvar}
oLang=$LANG oLcAll=$LC_ALL
LANG=C LC_ALL=C
bytlen=${#myvar}
printf -v myreal "%q" "$myvar"
LANG=$oLang LC_ALL=$oLcAll
printf "%s has %d chars, %d bytes: (%s).\n" "${myvar}" $chrlen $bytlen "$myreal"

will answer: 会回答:

Généralités has 11 chars, 14 bytes: ($'G\303\251n\303\251ralit\303\251s').

Nota: According to Isabell Cowan's comment , I've added setting to $LC_ALL along with $LANG . Nota:根据Isabell Cowan的评论 ,我在$LC_ALL$LANG添加了设置。

Length of an argument 参数长度

Argument work same as regular variables 参数工作与常规变量相同

strLen() {
    local bytlen sreal oLang=$LANG oLcAll=$LC_ALL
    LANG=C LC_ALL=C
    bytlen=${#1}
    printf -v sreal %q "$1"
    LANG=$oLang LC_ALL=$oLcAll
    printf "String '%s' is %d bytes, but %d chars len: %s.\n" "$1" $bytlen ${#1} "$sreal"
}

will work as 将作为

strLen théorème
String 'théorème' is 10 bytes, but 8 chars len: $'th\303\251or\303\250me'

Useful printf correction tool: 有用的printf校正工具:

If you: 如果你:

for string in Généralités Language Théorème Février  "Left: ←" "Yin Yang ☯";do
    printf " - %-14s is %2d char length\n" "'$string'"  ${#string}
done

 - 'Généralités' is 11 char length
 - 'Language'     is  8 char length
 - 'Théorème'   is  8 char length
 - 'Février'     is  7 char length
 - 'Left: ←'    is  7 char length
 - 'Yin Yang ☯' is 10 char length

Not really pretty ... For this, there is a little function: 不太漂亮 ...为此,有一个小功能:

strU8DiffLen () { 
    local bytlen oLang=$LANG oLcAll=$LC_ALL
    LANG=C LC_ALL=C
    bytlen=${#1}
    LANG=$oLang LC_ALL=$oLcAll
    return $(( bytlen - ${#1} ))
}

Then now: 那么现在:

for string in Généralités Language Théorème Février  "Left: ←" "Yin Yang ☯";do
    strU8DiffLen "$string"
    printf " - %-$((14+$?))s is %2d chars length, but uses %2d bytes\n" \
        "'$string'" ${#string} $((${#string}+$?))
  done 

 - 'Généralités'  is 11 chars length, but uses 14 bytes
 - 'Language'     is  8 chars length, but uses  8 bytes
 - 'Théorème'     is  8 chars length, but uses 10 bytes
 - 'Février'      is  7 chars length, but uses  8 bytes
 - 'Left: ←'      is  7 chars length, but uses  9 bytes
 - 'Yin Yang ☯'   is 10 chars length, but uses 12 bytes

But there left some strange UTF-8 behaviour, like double-spaced chars, zero spaced chars, reverse deplacement and other that could not be as simple... Have a look at diffU8test.sh or diffU8test.sh.txt for more limitations. 但是还有一些奇怪的UTF-8行为,例如双倍行距字符,零行距字符,反向移位以及其他可能不那么简单的...请查看diffU8test.shdiffU8test.sh.txt以了解更多限制。


#6楼

I would use something like this: 我会用这样的东西:

var2=$(echo $myvar | wc -c)

You don't need a script. 您不需要脚本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值