-3

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.

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
Aya Noaman
  • 154
  • 1
  • 1
  • 10
  • 1
    Do you mean the `length()` of the string or the amount of non-whitespace characters by only counting _word_ characters? – Glains Aug 21 '20 at 10:54
  • 1
    [How to check how many letters are in a string in java?](https://stackoverflow.com/questions/7252142/how-to-check-how-many-letters-are-in-a-string-in-java/) This question is already answered, you can check here. – Raj Aug 21 '20 at 10:58
  • thanks, as i mentioned i am terrible at searching :) – Aya Noaman Aug 21 '20 at 11:01
  • @Glains all of the characters including spaces – Aya Noaman Aug 21 '20 at 11:01

2 Answers2

3

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.

JustAnotherDeveloper
  • 1,825
  • 2
  • 8
  • 22
2

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

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