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
如何为输出11
设置另一个变量?
How do you set another variable to the output 11
?
推荐答案
UTF-8 字符串长度
除了fedorqui 的正确答案,我还想展示string 长度和字节长度:
UTF-8 string length
In addition to fedorqui's correct answer, I would like to show the difference between string length and byte length:
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.
您甚至可以查看存储的字符:
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).
" "${myvar}" $chrlen $bytlen "$myreal"
会回答:
Généralités has 11 chars, 14 bytes: ($'G303251n303251ralit303251s').
注意:根据 Isabell Cowan 的评论,我已经将设置添加到 $LC_ALL
和 $LANG
.
Nota: According to Isabell Cowan's comment, I've added setting to $LC_ALL
along with $LANG
.
参数与常规变量相同
showStrLen() {
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"
}
将作为
showStrLen théorème
String 'théorème' is 10 bytes, but 8 chars len: $'th303251or303250me'
好用的printf
修正工具:
如果你:
Useful printf
correction tool:
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
" "'$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
不是很漂亮的输出!
为此,这里有一个小功能:
For this, here is a little function:
strU8DiffLen() {
local charlen=${#1} LANG=C LC_ALL=C
return $(( ${#1} - charlen ))
}
或写成一行:
strU8DiffLen() { local chLen=${#1} LANG=C LC_ALL=C;return $((${#1}-chLen));}
那么现在:
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 行为,比如双倍行距字符、零行距字符、反向置换和其他不那么简单的......
Unfortunely, this is not perfect!
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...
看看 diffU8test.sh 或 diffU8test.sh.txt 了解更多限制.
Have a look at diffU8test.sh or diffU8test.sh.txt for more limitations.
这篇关于bash中字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!