VBA 中的哪个命令可以计算字符串变量中的字符数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1725089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:07:42  来源:igfitidea点击:

Which command in VBA can count the number of characters in a string variable?

vbacountvariablescommandcharacter

提问by brilliant

Let's say I have this variable:

假设我有这个变量:

word = "habit"

word = "习惯"

which command in VBA will allow me to count how many characters are there in this variable (in my case it's 5).

VBA 中的哪个命令将允许我计算这个变量中有多少个字符(在我的例子中是 5)。

Important: the variable "word" contains only one word, no spaces, but may have contain numbers and hyphens.

重要提示:变量“word”只包含一个词,没有空格,但可能包含数字和连字符。

回答by Ben McCormack

Do you mean counting the number of characters in a string? That's very simple

你的意思是计算字符串中的字符数?这很简单

Dim strWord As String
Dim lngNumberOfCharacters as Long

strWord = "habit"
lngNumberOfCharacters = Len(strWord)
Debug.Print lngNumberOfCharacters

回答by dxh

Len(word)

Although that's not what your question title asks =)

虽然这不是你的问题标题所要求的 =)

回答by Austin Salonen

Len is what you want.

莱恩就是你想要的。

word = "habit"  
length = Len(word)

回答by Robert Harvey

Use the Len function

使用 Len 函数

length = Len(myString)

回答by user5053510

Try this:

尝试这个:

word = "habit"
findchar = 'b"
replacechar = ""
charactercount = len(word) - len(replace(word,findchar,replacechar))