2011-06-10 73 views
25

我有一个字符串,它是这样的:1|"value"|;用|分割字符串分离器在java中

我想拆分字符串,并选择|作为分隔符。

我的代码如下所示:

String[] separated = line.split("|"); 

我得到的是一个包含所有字符作为一个项的数组:

separated[0] = "" 
separated[1] = "1" 
separated[2] = "|" 
separated[3] = """ 
separated[4] = "v" 
separated[5] = "a" 
... 

有谁知道为什么吗?
我不能用|分割字符串吗?

回答

62

|在RegEx中被视为OR。所以,你需要逃避它:

String[] separated = line.split("\\|"); 
+0

我猜对了,...非常感谢! – Prexx 2011-06-10 11:30:56

+4

如果你的分隔符是动态的,'line.split(“\\”+ separator)''不能和';'或','一起工作。另一个解决方案是'line.split(“[”+ separator +“]”)',因为括号中的字符也会被转义。 – 2015-09-02 07:26:22

3

试试这个:String[] separated = line.split("\\|");

我的回答是好。我纠正了“分离”的拼写:) :)

此外,原因是什么? |表示正则表达式中的“或”。你需要逃避它。

+0

:P非常感谢 – Prexx 2011-06-10 11:31:57

2

String.split()使用正则表达式,所以你需要转义'|'像.split(“\\ |”);

9

你必须逃避|,因为它在正则表达式中有特殊的含义。看看split(..)方法。

String[] sep = line.split("\\|"); 

第二\用来逃避|和第一\用于转义第二\ :)。

3

逃离管道。有用。

String.split("\\|"); 

管道是正则表达式的含义或特殊字符

4

split方法的参数是一个正则表达式,你可以阅读here。由于|在正则表达式中有特殊含义,因此您需要将其转义。该代码然后看起来像这样(如其他人已经显示):

String[] separated = line.split("\\|"); 
3

它不会这样工作,因为你必须逃避管|第一。 以下示例代码(http://www.rgagnon.com/javadetails/java-0438.html)中显示了一个示例。

public class StringSplit { 
    public static void main(String args[]) throws Exception{ 
    String testString = "Real|How|To"; 
    // bad 
    System.out.println(java.util.Arrays.toString(
     testString.split("|") 
    )); 
    // output : [, R, e, a, l, |, H, o, w, |, T, o] 

    // good 
    System.out.println(java.util.Arrays.toString(
     testString.split("\\|") 
    )); 
    // output : [Real, How, To] 
    } 
} 
0

|意思是在正则表达式中,你应该逃避它。更重要的是,一个'\',你会得到'\'在Java字符串中什么都没有。所以你也应该逃避产生'\'的'\'本身。

祝你好运!

0
public class StringUtil { 

    private static final String HT = "\t"; 
    private static final String CRLF = "\r\n"; 

    // This class cannot be instantiated 
    private StringUtil() { 
    } 

    /** 
    * Split the string into an array of strings using one of the separator in 
    * 'sep'. 
    * 
    * @param s 
    *   the string to tokenize 
    * @param sep 
    *   a list of separator to use 
    * 
    * @return the array of tokens (an array of size 1 with the original string 
    *   if no separator found) 
    */ 
    public static String[] split(final String s, final String sep) { 
    // convert a String s to an Array, the elements 
    // are delimited by sep 
    final Vector<Integer> tokenIndex = new Vector<Integer>(10); 
    final int len = s.length(); 
    int i; 

    // Find all characters in string matching one of the separators in 'sep' 
    for (i = 0; i < len; i++) 
     if (sep.indexOf(s.charAt(i)) != -1) 
     tokenIndex.addElement(new Integer(i)); 

    final int size = tokenIndex.size(); 
    final String[] elements = new String[size + 1]; 

    // No separators: return the string as the first element 
    if (size == 0) 
     elements[0] = s; 
    else { 
     // Init indexes 
     int start = 0; 
     int end = (tokenIndex.elementAt(0)).intValue(); 
     // Get the first token 
     elements[0] = s.substring(start, end); 

     // Get the mid tokens 
     for (i = 1; i < size; i++) { 
     // update indexes 
     start = (tokenIndex.elementAt(i - 1)).intValue() + 1; 
     end = (tokenIndex.elementAt(i)).intValue(); 
     elements[i] = s.substring(start, end); 
     } 
     // Get last token 
     start = (tokenIndex.elementAt(i - 1)).intValue() + 1; 
     elements[i] = (start < s.length()) ? s.substring(start) : ""; 
    } 

    return elements; 
    } 

} 
2

你可以用劈裂之前像“#”另一个字符替换管道,试试这个

String[] seperated = line.replace('|','#').split("#");