问题

如何获取存储在变量中的字符串的长度并将其分配给另一个变量?

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

如何将另一个变量设置为输出11?

  最佳答案

UTF-8字符串长度

除了 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.
" "${myvar}" $chrlen $bytlen
 

将呈现:

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

您甚至可以看到存储字符:

 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).
" "${myvar}" $chrlen $bytlen "$myreal"
 

将回答:

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

Nota:根据 Isabell Cowan的评论,我将设置添加到$LC_ALL$LANG.

参数的长度

参数与常规变量相同

 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.
" "$1" $bytlen ${#1} "$sreal"
}
 

将作为

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

有用的printf校正工具:

如果你:

 for string in Généralités Language Théorème Février  "Left: ←" "Yin Yang ☯";do
    printf " - %-14s is %2d char length
" "'$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
 

不真的很好...对于这个,有一个小功能:

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

现在:

 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
" \
        "'$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
 

但是留下了一些奇怪的UTF-8行为,如double-might chars,零行距字符,反向取消放置以及其他不太简单的行为.......看 visumU8test.sh visuU8test.sh以获得更多的限制.

  相同标签的其他问题

bashvariablesstring-length