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

したがって、値は次のようになります。

2012年6月27日、2012年5月7日など

このために何らかの正規表現を作成しようとしましたが、成功しませんでした。

私が試した例:

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

私の理解が正しければ、分割して 1 回の操作で置き換えようとしています。しないでください。

それを受け入れる場合、必要なのはシーケンスを適切な文字に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

問題は charset にあります。デコードする必要があります。

于 2012-07-09T12:10:55.010 に答える
2
String[] tokens = phrase.replaceAll("%2F","/").split("%2C");
于 2012-07-09T12:14:50.657 に答える
0

怠惰な方法は、すべての文字列を必要な値に置き換えることです:

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 に答える