0

我有一个这样的字符串:

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/052012 等

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

我试过的例子:

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);
4

5 回答 5

8

如果我理解正确,您正在尝试使用一项操作进行拆分和替换。不。

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

于 2012-07-09T12:12:07.097 回答
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);
    }
于 2012-07-09T12:16:24.487 回答
2

您的麻烦在于字符集,您必须对其进行解码。

于 2012-07-09T12:10:55.010 回答
2
String[] tokens = phrase.replaceAll("%2F","/").split("%2C");
于 2012-07-09T12:14:50.657 回答
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.... ;
于 2012-07-09T12:12:09.350 回答