Write some Java statements that use the String methods indexOf and substring to
ID: 3613611 • Letter: W
Question
Write some Java statements that use the String methods indexOf and substring to find the first word in thestring. We define word to be a string of characters that does notinclude whitespace. For example the first word of the string "Hello, my goodfriend!" is the string "Hello," andthe second word is the string "my". Write some Java statements that use the String methods indexOf and substring to find the first word in thestring. We define word to be a string of characters that does notinclude whitespace. For example the first word of the string "Hello, my goodfriend!" is the string "Hello," andthe second word is the string "my".Explanation / Answer
publicString getFirstWord(String input) { if(input.indexOf('')>0) { return input.substring(0,input.indexOf(' ')); } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.