CodeGym /Courses /Java Multithreading /Finding, retrieving, and deleting substrings

Finding, retrieving, and deleting substrings

Java Multithreading
Level 2 , Lesson 4
Available

"Check out some other things you can do with substrings:"

8) How do I find a substring?

The indexOf and lastIndexOf methods let you search for strings within strings. There are 4 versions of these methods:

The indexOf method looks for a string in a specified String. The method can search for the string from the beginning of the specified String, or starting from some index (the second method). If the string is found, then the method returns the index of its first character; if it is not found, then it returns -1.

Method(s) Example(s)
int indexOf(String str)
String s = "Good news, everyone!";
int index = s.indexOf ("ne");
Result:

index == 5
int indexOf(String str, int from)
String s = "Good news, everyone!";
int index = s.indexOf ("ne", 7);
Result:

index == 17

"The lastIndexOf method searches for the specified string backwards from the end of our String! This method can search for a string from the very end of our String, or starting from some index (the second method). If the string is found, then the method returns the index of its first character; if it is not found, then it returns -1."

Method(s) Example(s)
int lastIndexOf(String str)
String s = "Good news, everyone!";
int index = s.lastIndexOf("ne");
Result:

index == 17
int lastIndexOf(String str, int from)
String s = "Good news, everyone!";
int index = s.lastIndexOf("ne", 15);
Result:

index == 5

9) How do I replace part of a String with another String?

"There are three methods for this."

The replace method replaces all occurrences of a particular character with another character.

The replaceAll method replaces all occurrences of a substring with another string.

The replaceFirst method replaces the first occurrence of a passed substring with a specified string.

Method(s) Example(s)
String replace(char oldChar, char newChar)
String s = "Good news, everyone!";
String s2 = s.replace>('o', 'a');
Result:

s2 == "Gaad news, everyane!";
String replaceAll(String regex, String replacement)
String s = "Good news, everyone!";
String s2 = s.replaceAll ("ne", "_");
Result:

s2 == "Good _ws, everyo_!";
String replaceFirst(String regex, String replacement)
String s = "Good news, everyone!";
String s2 = s.replaceFirst ("ne", "_");
Result:

s2 == "Good _ws everyone!";

"But you need to be careful with these. In the last two methods (replaceAll and replaceFirst), the string we are looking for is passed as a regular expression, not a simple string. But I'll talk about that later."

Comments (7)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
MaGaby2280 Level 41, Guatemala City, Guatemala
24 June 2020
Good refreshment of basic concepts! ;o)
Ewerton Level 30, Belo Horizonte, Brasil
10 July 2019
I think this lesson would be better used on the first levels.
Hossein Shams Level 1, Atlanta, United States
17 October 2019
Truly said. I already used almost all of these methods in previous tasks!
yz Level 37, Jakarta, Indonesia
18 December 2019
cause they want us to learn the whole philosophy of codegym is that
Seb Level 41, Crefeld, Germany
15 January 2020
Yeah, I wonder how the bigger tasks should have been solved without these tools... I too think that it would be helpful for a complete beginner to have this information available earlier already. But hey, never mind - at least there is a little thing that can still be improved. Apart from that I think that this whole experience here - together with our captain John Squirrels, Ellie, Kim, Bilaabo, Professor Noodles, Diego, Julio Siesta and "uncle" Rishi is absolutely amazing! A big shout out to the crew therefore! ;-) :-)
BlueJavaBanana Level 37
4 July 2020
But it is nice every now and then to not have your mind hurt after a lesson!
31 March 2021
Unfortunately, I went Java Collections way first, so totally agree.