How can I find out the number of characters in a String in Java?

225 Views Asked by At

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.

2

There are 2 best solutions below

0
JustAnotherDeveloper On BEST ANSWER

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.

0
Sorin Certan On

Use the .length() to get the number of characters in a String

  String word = "Hippo";
  int numOfCharacters = word.length();