如何在Java中查找字符串中的字符数?

huangapple go评论88阅读模式
英文:

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();

huangapple
  • 本文由 发表于 2020年8月21日 18:52:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63521458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定