英文:
How can I find out the number of characters in a String in Java?
问题
这是一个简单的问题,但我在研究方面不太擅长。
我正在尝试找出String
中的字符数,并将其放入一个int
变量中。例如:
String word = "Hippo";
int numOfCharacters = (告诉我单词中有多少个字符的代码);
然后以后可以使用那个数字。
英文:
This is a simple question but I'm not very good at research.
I'm trying to find out the number of characters in a String
and put it in an int
variable. For example:
String word = "Hippo";
int numOfCharacters = (the code that tells me how many characters in the word);
And then use that number later.
答案1
得分: 3
relevant JavaDoc会告诉你,length()
是你要寻找的方法。因此,word.length();
会返回word
的大小,这正是你想要的。
英文:
The relevant JavaDoc will tell you that length()
is the method you're looking for. So word.length();
would return the size of word
, which is what you want.
答案2
得分: 2
使用 .length() 来获取字符串中字符的数量
String word = "Hippo";
int numOfCharacters = word.length();
英文:
Use the .length() to get the number of characters in a String
String word = "Hippo";
int numOfCharacters = word.length();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论