Get the Length of a String in Java

Revision as of 18:04, 6 January 2016 by Kipkis (Kipkis | contribs) (importing article from wikihow)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Steps

  1. Check to see if the string is null. This is usually neglected and could cause trouble as the next call will trigger an exception.
  2. 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