Challenge for More String Methods

Challenge

In Java, methods of an Object can be chained together, so you can perform multiple operations in one line of code. For example:

String word = "Startling";
int len = word.substring(0, 5).length();
System.out.println(len);

This will print "5". The word.substring call returns the String "Start". The method length() is then called on "start" and returns 5 to len.

You came to your desk and see that your boss left the following note:

I was typing out some snacks, but accidentally pressed 'x' instead of 'c'. Can you fix my typo and replace all the 'x's with 'c's? Also, please split the String into an array of 5 words called snacks. And do this in one line of code! Thanks.

You are given this String:

 String words = "xhoxolate xookies xandy xake xupxakes";

And need to call some methods on the String so it return an array to snacks that will look like this: {"chocolate", "cookies", "candy", "cake", "cupcakes"}

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Contact Us
Sign in or email us at [email protected]