CodeGym /Java Course /Java Multithreading /StringTokenizer, String.replace, and String.split

StringTokenizer, String.replace, and String.split

Java Multithreading
Level 2 , Lesson 12
Available

"Hi, Amigo!"

"As far as I know, Rishi has already told you about regular expressions."

"Yes, it was very interesting."

"Great, now I'll tell you about using regular expressions to work with Strings."

"Let's start with the simplest question:"

1) How do I check to see if a String matches the pattern specified by a regular expression?

"There is a matches method for this. You pass a String containing a regular expression, and the method returns true or false."

Method(s) Example(s)
boolean matches(String regex)
String s = "Good news, everyone!";
Boolean test = s.matches("news\\.*");
Result:

false (the String doesn't start with "news")

2) How do I replace all matching substrings with different strings?

"There are two methods for this."

"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 replaceAll(String regex, String replacement)
String s = "Good news, everyone!";
String s2 = s.replaceAll ("e\\.","EX");
Result:

s2 == "Good nEXs EXEXyonEX";
String replaceFirst(String regex, String replacement)
String s = "Good news, everyone!";
String s2 = s.replaceFirst("e\\.","EX");
Result:

s2 == "Good nEXs, everyone!";

3) How do I split a string into parts?

"For this, we have the split method, which takes a delimiting mask:"

Method(s) Example(s)
String[] split(String regex)
String s = "Good news everyone!";
String[] ss = s.split("ne");
System.out.println(Arrays.toString(ss));
Result (an array of three strings):

[Good , ws everyo, !]
"Good ", "ws everyo", "!";

"The StringTokenizer class is another way to split a string into parts."

"This class doesn't use regular expressions. Instead, you simply pass in a String containing a set of delimiters. The advantage of this approach is that it doesn't break the entire string into pieces all at once, instead it slowly moves from the beginning to the end."

"The class consists of a constructor and two methods. You need to pass the String we are breaking apart into the constructor, along with a String containing the set of delimiting characters."

The nextToken method returns the next token (substring).

The hasMoreTokens() method returns true if there are still substrings that haven't been returned yet.

Method(s) Example(s)
boolean hasMoreTokens()

String nextToken()
String s = "Good news, everyone!";

StringTokenizer tokenizer =
new StringTokenizer(s,"ne");
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
System.out.println(token);
}
Screen output:

Good
ws
v
ryo
!

"Note that any character in the second String passed to the StringTokenizer constructor is treated as a delimiter."

"Once again, everything seems clear. I might not be able to write this code all by myself right away, but I understand what's going on here."

"Excellent, then we'll assume that you've mastered the topic."

Comments (7)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Thomas Level 41, Bayreuth, Germany
2 June 2020
This

s.replaceAll ("e\\.","EX");
probably should read

s.replaceAll ("e.","EX");
escaping the point would match a point and not any single character
Seb Level 41, Crefeld, Germany
7 February 2021
Yes, you're exactly right. The same accounts for the line below:

String s2 = s.replaceFirst("e\\.","EX");
should actually be:

String s2 = s.replaceFirst("e.","EX");
Jason Level 26, Rancho Cucamonga, United States
18 January 2020
so wonderful that you are teaching us the stuff we were "supposed to look up and google on our own so we can learn". You know if you just teach this stuff in the right order you might have a program actually worth following through on. Also, FYI it's 2020 I think people know how to google and don't need to be forced to do so. I'd really consider teaching this before you are asking any of your students to do string manipulation or at least anything that involves more than basic concatenation.
Misiu Level 41, Gdansk, Poland
6 May 2020
A little strange learning. First: solve these tasks. Next, after a couple of lessons: here are methods and ways you should have been used in those previous tasks, we know you already know them, but never mind...
Denis Level 25, Vancouver, Canada
12 June 2019
Are you sure about the last example ? may be the result will be: Good ws, everyo !
kiwii Level 37, United States
25 June 2019
The result is correct. You could check it by running the code in IntelliJ. The delimiters "ne" = "n" or "e" or "ne". It is a simpler way to split strings with multiple delimiters. But is not a recommended way. Better use Regular Expression I guess. Thanks for bringing this to our attention! I would not think it twice without your comment.
Seb Level 41, Crefeld, Germany
5 February 2020
Yes, thanks for this clarification. It's not really directly obvious that the delimiter "ne" can be "n", "e" or "ne". And actually, the example output is also not entirely correct as it is missing the comma "," after "ws". When you run the example, the output will be as follows: Good ws, v ryo !