More String Methods


Collapse Content

Below are 3 additional String methods. After reviewing them, you should be able to solve the challenges and tasks below and the challenges in String Programming Practice.

indexOf
indexOf(c) - Finds the character 'c' within the String and returns the index where 'c' first occurs. For example:

 int value = "abcaz".indexOf('a'); 

value will equal 0, the index of the first 'a' in the String. This method can also be used to get the indexOf a substring:

value = "abcaz".indexOf("az");

This will set value to 3 since "az" starts at index 3.

replace
replace(oldChar, newChar) - Returns a new string resulting from replacing all occurrences of oldChar with newChar. For example:

String verb = "food".replace('o', 'e');

verb will equal "feed". This method can also be used to replace Strings instead of characters:

 String exclamation = "hello? what the hell? hell find out!";
 String clean = exclamation.replace("hell", "heck");

clean will now equal "hecko? what the heck? heck find out!".

split
This method is used to split a String at specific substrings. split(" ") will split the String at every space character (deleting each space), and return the smaller Strings as a String array. For example:

String[] words = "the quick fox".split(" ");

Will set words to the array: {"the", "quick", "fox"}.

You can also pass in other Strings to split a String by (or even other patterns). This line of code:

words = "helloYObyeYO".split("YO");

...will now set words to {"hello", "bye"}.

Task

  1. Create a method find which takes in one String parameter and returns the first location (as an integer) of that String in the Note content.
  2. Create a method replace which takes in two String parameters (oldText and newText) and replaces all occurrences of oldText in the content with newText.
  3. Bonus - Create a method wordCount which return the number of words in the content. A Word is a series of characters without spaces separated by one or more spaces from another series of characters. (You do not need to worry about newlines.)

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.

Comments

  • This is my code for third BONUS task. But it did not pass test. What is wrong ?

    public int wordCount()
    {
    String[] words = content.split(" ");
    int i = words.length;
    return i;
    }

  • How does it handle words separated by one or more spaces?

  • This bonus task could be solved in different ways, but the simplest (though not covered) would be to split the text on a regex pattern for one or more white spaces.

  • It's a pity you can't get the answer somewhere on the site.

    I try
    String[] snacks = words.replace('x' , 'c').split(",");

    and it is not correct, but how would I ever find out what I'm doing wrong?

  • Ok the [] were bad, because it's not an array, of course. But I'm still stumped.

  • Your answer was very close but do you want to split the strings by commas or spaces?

  • Let's not go there, OK?

    (Kidding. Of course. Thank you for the hint.)

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