In Java, the String.split(String regex) method is used to split a string around matches of the given regular expression. The argument regex is a string that specifies the delimiter according to regular expression syntax.
Here are some examples of how to use String.split with different delimiters:
To split a string into substrings using space as a delimiter:
String text = "Hello World From Java"; String[] words = text.split(" "); // Splits the string around spaces To split a string by commas:
String csv = "Apple,Orange,Banana"; String[] fruits = csv.split(","); // Splits the string around commas The dot is a special character in regular expressions, representing any character. To use a dot as a delimiter, you must escape it:
String dottedString = "192.168.1.1"; String[] parts = dottedString.split("\\."); // Escape the dot The pipe symbol | is also a special character in regular expressions (it means logical OR). To use it as a delimiter, it must be escaped:
String data = "One|Two|Three"; String[] items = data.split("\\|"); // Escape the pipe symbol You can use more complex regular expressions as delimiters. For example, to split a string on one or more spaces:
String multiSpaceText = "Hello World From Java"; String[] words = multiSpaceText.split("\\s+"); // Splits around one or more spaces String.split can also limit the number of substrings to return:
String csv = "Apple,Orange,Banana,Kiwi"; String[] limitedFruits = csv.split(",", 3); // Limit to 3 substrings In this case, limitedFruits will contain ["Apple", "Orange", "Banana,Kiwi"].
split method treats consecutive delimiters as separate delimiters. If you want to treat consecutive delimiters as one, you may need to use a regular expression that groups them.. | [ ] { } ( ) * + ? ^ $) and escape them if you want to use them as literal characters.youtube-api android-data-usage scala citations coordinator-layout mongorepository subset use-effect eclipse-plugin public