-1
String str = "AlwinX-road-9:00pm-kanchana travels-25365445421";
String[] names = str.split("-");

我想要如下输出:

AlwinX-road
9:00 PM
kanchana 旅行
25365445421

4

7 回答 7

2
   Use pattern matching to match your requirement
    String str = "AlwinX-road-9:00pm-kanchana travels-25365445421"; 
    String regex = "(^[A-Z-a-z ]+)[-]+(\\d+:\\d+pm)[-]([a-z]+\\s+[a-z]+)[-](\\d+)";
    Matcher matcher = Pattern.compile( regex ).matcher( str);
    while (matcher.find( ))
    {
    String roadname = matcher.group(1);
    String time = matcher.group(2);
    String travels = matcher.group(3);
    String digits= matcher.group(4);
    System.out.println("time="+time);
    System.out.println("travels="+travels);
    System.out.println("digits="+digits);                           
    }
于 2012-10-04T17:18:56.083 回答
0

Since you want to include the delimiter in your first output line, you can do the split, and merge the first two element with a -: -

String[] names = str.split("-");

System.out.println(names[0] + "-" + names[1])
for (int i = 2;i < names.length; i++) {
    System.out.println(names[i])
}
于 2012-10-04T16:30:01.333 回答
0

The split() method can't distinguish the dash in AlwinX-road and the other dashes in the string, it treats all the dashes the same. You will need to do some sort of post processing on the resulting array. If you will always need the first two strings in the array joined you can just do that. If your strings are more complex you will need to add additional logic to join the strings in the array.

于 2012-10-04T16:31:59.940 回答
0

One way you could do it, assuming the first '-' is always part of a two part identifier.

String str = "AlwinX-road-9:00pm-kanchana travels-25365445421";
String[] tokens = str.split("-");
String[] output = new String[tokens.length - 1];
output[0] = tokens[0] + '-' + tokens[1];
System.out.println(output[0]);
for(int i = 1; i < output.length; i++){ 
  output[i] = tokens[i+1];
  System.out.println(output[i]);
}
于 2012-10-04T16:44:13.327 回答
0

Looks like you want to split (with removal of all dashes but the first one).

String str = "AlwinX-road-9:00pm-kanchana travels-25365445421";
String[] names = str.split("-");

for (String value : names)
{
    System.out.println(value);
}

So its produces:

AlwinX
road
9:00pm
kanchana travels
25365445421

Notice that "AlwinX" and "road" we split as well since they had a dash in between. So you will need custom logic to handle this case. here is an example how to do it (I used StringTokenizer):

StringTokenizer tk = new StringTokenizer(str, "-", true);
String firstString = null;
String secondString = null;
while (tk.hasMoreTokens())
{
    final String token = tk.nextToken();
    if (firstString == null)
    {
        firstString = token;
        continue;
    }
    if (secondString == null && firstString != null && !token.equals("-"))
    {
        secondString = token;
        System.out.println(firstString + "-" + secondString);
        continue;
    }
    if (!token.equals("-"))
    {
        System.out.println(token);
    }
}

This will produce:

AlwinX-road
9:00pm
kanchana travels
25365445421
于 2012-10-04T16:44:17.873 回答
0

from your format, I think you want to split the first one just before the time part. You can do it this way:

String str =yourString;
String beforetime=str.split("-\\d+:\\d+[ap]m")[0]; //this is your first token, 
                                                    //AlwinX-road in your example
String rest=str.substring(beforetime.length()+1);
String[] restNames=rest.split("-");

If you really need it all together in one array then see the code below:

String[] allTogether=new String[restNames.length+1];//the string with all your tokens
allTogether[0]=beforetime;
System.arraycopy(restNames, 0, allTogether, 1, restNames.length);
于 2012-10-04T16:44:43.193 回答
-1

如果您使用“_”作为分隔符而不是“-”:AlwinX-road_9:00pm_kanchana travels_25365445421

新代码:

String str = new String("AlwinX-road_9:00pm_kanchana travels_25365445421");
String separator = new String("_");
String[] names = str.split(separator);
for(int i=0; i<names.length; i++){
    System.out.println(names[i]);
}
于 2012-10-04T16:49:27.533 回答