string manipulation- middle 3 letters

Chickadee picture Chickadee · Jun 1, 2011 · Viewed 9.1k times · Source

After prompting for a string, I am trying to display the middle three characters of that string. How would I do that?

this is what I tried:

String middle3 = (string.length < 3) ? null : string.substring(string.length / 2 - 1), string.length / 2 + 2);

Answer

codaddict picture codaddict · Jun 1, 2011

length is a method of the String class, so you need to use ():

String middle3 = (string.length() < 3) ? null : 
              string.substring(string.length() / 2 - 1, string.length() / 2 + 2);