Get the Length of a String in Java
Java treats Strings as objects not as simple data types. This article will show you how to get the length of a string in Java.
Contents
Steps
- Check to see if the string is null. This is usually neglected and could cause trouble as the next call will trigger an exception.
- Use the length() instance method, available in every string to get an integer with the length of the string you called length() from.
Code Sample
Tips
- For the String of length n, the index for characters of the string are 0, 1,..., n-1.
Related Articles
- Call a Method in Java
- Print Double Quotes in Java
- Use Graphics in a Java Applet
- Add JARs to Project Build Paths in Eclipse (Java)
- Generate a Random Number in Java