To split a string that contains only delimiters in Java, you can use the `split()` method provided by the `String` class. This method takes a regular expression as an argument, so you can use a regular expression that matches one or more occurrences of the delimiter.

Here are 6 examples to demonstrate splitting a string with only delimiters:

Example 1:

             java
String input = ",";
String[] parts = input.split(",");
System.out.println(Arrays.toString(parts));

             

– Explanation:

– The input string is `","`, which contains only a comma delimiter.

– The `split(",")` method is called to split the input string using the comma delimiter.

– The result is stored in the `parts` array.

– The output will be: `[, ]` (an empty string before and after the delimiter)

Example 2:

             java
String input = "||";
String[] parts = input.split("\\|\\|");
System.out.println(Arrays.toString(parts));

             

– Explanation:

– The input string is `"||"`, which contains only two pipe delimiters.

– Since `|` is a special character in regular expressions, it needs to be escaped with a backslash to be treated as a literal character.

– The `split("\\|\\|")` method is called to split the input string using the double pipe delimiter.

– The output will be: `[, , ]` (three empty strings between the delimiters)

Example 3:

             java
String input = "///";
String[] parts = input.split("/");
System.out.println(Arrays.toString(parts));

             

– Explanation:

– The input string is `"///"`, which contains three forward slash delimiters.

– The `split("/")` method is called to split the input string using the forward slash delimiter.

– The output will be: `[, , , ]` (four empty strings, one for each delimiter and one at the start)

Example 4:

             java
String input = "**";
String[] parts = input.split("\\*");
System.out.println(Arrays.toString(parts));

             

– Explanation:

– The input string is `"**"`, which contains two asterisk delimiters.

– The `split("\\*")` method is called to split the input string using the asterisk delimiter.

– The output will be: `[, , ]` (three empty strings between the delimiters)

Example 5:

             java
String input = "%%%";
String[] parts = input.split("%");
System.out.println(Arrays.toString(parts));

             

– Explanation:

– The input string is `"%%%"`, which contains three percent delimiters.

– The `split("%")` method is called to split the input string using the percent delimiter.

– The output will be: `[, , , ]` (four empty strings, one for each delimiter and one at the start)

Example 6:

             java
String input = "~~~~";
String[] parts = input.split("~");
System.out.println(Arrays.toString(parts));

             

– Explanation:

– The input string is `"~~~~"`, which contains four tilde delimiters.

– The `split("~")` method is called to split the input string using the tilde delimiter.

– The output will be: `[, , , , ]` (five empty strings, one for each delimiter and one at the start)

Overall Output:

             
Example 1: [, ]
Example 2: [, , ]
Example 3: [, , , ]
Example 4: [, , ]
Example 5: [, , , ]
Example 6: [, , , , ]

             

In these examples, the split method separates the empty substrings created by the delimiters and stores them in the resulting array.

Was this article helpful?
YesNo

Similar Posts