在java中拆分字符串

时间:2012-07-09 12:07:28

标签: java regex string

我有一个像这样的字符串:

06%2F27%2F2012%2C07%2F05%2F2012%2C06%2F29%2F2012%2C07%2F05%2F2012%2C07%2F06%2F2012%2C07%2F19%2F2012%2C07%2F06%2F2012%2C07%2F19%2F2012%2C07%2F05%2F2012%2C07%2F05%2F2012%2C07%2F20%2F2012%2C07%2F20%2F2012%2C07%2F27%2F2012%2C08%2F02%2F2012%2C07%2F20%2F2012%2C07%2F26%2F2012%2C08%2F03%2F2012%2C08%2F06%2F2012%2C08%2F06%2F2012%2C08%2F17%2F2012

我希望它像这样分割它的值:

%2F should be /

%2C marks the starting of new string

所以价值观就像:

06/27 / 2012,07 / 05/05等

我尝试为此创建一些正则表达式,但没有成功。

我试过的例子:

String phrase = "06%2F27%2F2012%2C07%2F05%2F2012%2C06%2F29%2F2012%2C07%2F05%2F2012%2C07%2F06%2F2012%2C07%2F19%2F2012%2C07%2F06%2F2012%2C07%2F19%2F2012%2C07%2F05%2F2012%2C07%2F05%2F2012%2C07%2F20%2F2012%2C07%2F20%2F2012%2C07%2F27%2F2012%2C08%2F02%2F2012%2C07%2F20%2F2012%2C07%2F26%2F2012%2C08%2F03%2F2012%2C08%2F06%2F2012%2C08%2F06%2F2012%2C08%2F17%2F2012";
String delims = "REGEX_GOES_HERE";
String[] tokens = phrase.split(delims);

5 个答案:

答案 0 :(得分:8)

如果我理解正确,你会尝试拆分并替换一个操作。不。

如果您接受,那么您需要的是URLDecoder调用,它将用适当的字符替换%..个序列,然后是带有字符串分隔符的简单split()。< / p>

答案 1 :(得分:3)

这应该这样做:

String string = "06%2F27%2F2012%2C07%2F05%2F2012%2C06%2F29%2F2012"; // etc...
    String decoded = null;
    try {
        decoded = URLDecoder.decode(string, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    String[] splitString = decoded.split(",");
    for (String i : splitString) {
        System.out.println(i);
    }

答案 2 :(得分:2)

你的麻烦在charset,你必须解码它。

答案 3 :(得分:2)

String[] tokens = phrase.replaceAll("%2F","/").split("%2C");

答案 4 :(得分:0)

懒惰的方法是用你需要的值替换你的所有String:

String toModify = "06%2F27%2F2012%2C07%2F05%2F2012%2C06%2F29%2F2012%2C07%2F05%2F2012%2C07%2F06%2F2012%2C07%2F19%2F2012%2C07%2F06%2F2012%2C07%2F19%2F2012%2C07%2F05%2F2012%2C07%2F05%2F2012%2C07%2F20%2F2012%2C07%2F20%2F2012%2C07%2F27%2F2012%2C08%2F02%2F2012%2C07%2F20%2F2012%2C07%2F26%2F2012%2C08%2F03%2F2012%2C08%2F06%2F2012%2C08%2F06%2F2012%2C08%2F17%2F2012"
String my result = toModify.replaceAll("%2F","/").replaceAll("%2C"," ").replaceAll.... ;