How to Split String in Java?
How to Split String in Java?
In Java, you can split a string into an array of substrings using the split method of the String class. Here's an example:
rust:
String str = "Hello World!";
String[] parts = str.split(" ");
for (String part : parts) {
System.out.println(part);
}
Output:
Hello
World!
The split method takes a regular expression as an argument and returns an array of substrings split along the matches of the regular expression. In the example above, the regular expression is a space character (" "), so the string is split into an array of two parts, "Hello" and "World!".
Note that the split method can also be called with a limit argument, which specifies the maximum number of substrings that should be returned. For example:
rust
String str = "Hello World! How are you?";
String[] parts = str.split(" ", 3);
for (String part : parts) {
System.out.println(part);
}
Output:
sql
Hello
World!
How are you?
In this case, the limit argument is 3, so the string is split into an array of three parts, "Hello", "World!", and "How are you?"
Get in touch with essay writing services for Knowledgeable blogs