Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string manipulation- middle 3 letters

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);
like image 524
Chickadee Avatar asked Jun 16 '26 17:06

Chickadee


1 Answers

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);
like image 99
codaddict Avatar answered Jun 18 '26 06:06

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!