Split string by character

I have a case when I do the following:

final String[] columns = row.split(delimiter.toString()); 

Where delimiter is the symbol.

This works fine when I need to split based on tabs, providing \t as a delimiter. However, when I want to split into a channel, I pass the separator | , and it does not work properly.

I read a few posts on how | is a special character that means null or empty, so it breaks down into every character that it comes across, although I don't want this behavior.

I could do a simple check in my code for this case case and work around the problem:

 if ("|".equals(delimiter.toString())) { columns = row.split("\\" + delimiter.toString()); } else { columns = row.split(delimiter.toString()); } 

But I did not know if there is an easier way to get around this. Also, are there any other special characters that act like | What do I need to consider?

+6
source share
2 answers

Try:

 import java.util.regex.Pattern; ... final String[] columns = row.split(Pattern.quote(delimiter.toString())); 

As for the other metacharacters, as they are called, here is a quote from String Literals :

This API also supports a number of special characters that affect pattern matching.

...

The metacharacters supported by this API are the following: <([{\ ^ - = $! |]})? * +. >

Cm:

+18
source
  • You can use StringUtils from the Apache Commons Lang, which is equipped with methods that allow plain text rather than regular expressions:

     public static String[] split(String str, char separatorChar) public static String[] split(String str, String separatorChars) 
  • You can also use the StringTokenzier class, which does not expect a regular expression as a delimiter.

+4
source

Source: https://habr.com/ru/post/944890/


All Articles