通过keword在java中拆分字符串

时间:2022-03-15 01:41:41

I am writing a code, in which i want to split the following string:

我正在编写一个代码,我想在其中拆分以下字符串:

String str = "{route=Head west on, some address, distance=0.2 km, duration=1 min}";

I want to separate the value for route, distance and duration.

我想分开路线,距离和持续时间的值。

3 个解决方案

#1


1  

Try some thing like this

尝试这样的事情

   String str = "{route=Head west on, some address, distance=0.2 km, duration=1 min}";
    String newStr=str.replaceAll("\\}","");
    String[] arr=newStr.split("route=");
    String[] arr1=arr[1].split("distance=");
    String route=arr1[0];
    String[] arr2=arr1[1].split("duration=");
    String  distance=arr2[0];
    String duration=arr2[1];

    System.out.println("route= "+route);
    System.out.println("distance= "+distance);
    System.out.println("duration= "+duration);

#2


1  

You can use Regular Expression for splitting a string:

您可以使用正则表达式来拆分字符串:

String str = "{route=Head west on, some address, distance=0.2 km, duration=1 min}";
String regEx = "(\\{route=|distance=|duration=|\\})";
// String regEx = "(route=|distance=|duration=)";
String[] splited = str.split(regEx);
for(String s: splited) {
  System.out.println(s);
} 

You can add any other keyword (just add it to regEx) without changing code!

您可以添加任何其他关键字(只需将其添加到regEx)而无需更改代码!

#3


0  

Try out using the StringTokenizer to split the string as below:

尝试使用StringTokenizer分割字符串,如下所示:

final String SPLIT_STR = ",";
  String str = "{route=Head west on, some address, distance=0.2 km, duration=1 min}"; 
  final StringTokenizer stToken = new StringTokenizer(
     str, SPLIT_STR);
  final String[] splitStr = new String[stToken.countTokens()];
  int index = 0;
  while(stToken.hasMoreElements()) {
     splitStr[index++] = stToken.nextToken();
  }
  for(index=0; index < splitStr.length; index++) {
     System.out.println("Tokenizer : " + splitStr[index]);
  }

}

Hope this will work for you.

希望这对你有用。

#1


1  

Try some thing like this

尝试这样的事情

   String str = "{route=Head west on, some address, distance=0.2 km, duration=1 min}";
    String newStr=str.replaceAll("\\}","");
    String[] arr=newStr.split("route=");
    String[] arr1=arr[1].split("distance=");
    String route=arr1[0];
    String[] arr2=arr1[1].split("duration=");
    String  distance=arr2[0];
    String duration=arr2[1];

    System.out.println("route= "+route);
    System.out.println("distance= "+distance);
    System.out.println("duration= "+duration);

#2


1  

You can use Regular Expression for splitting a string:

您可以使用正则表达式来拆分字符串:

String str = "{route=Head west on, some address, distance=0.2 km, duration=1 min}";
String regEx = "(\\{route=|distance=|duration=|\\})";
// String regEx = "(route=|distance=|duration=)";
String[] splited = str.split(regEx);
for(String s: splited) {
  System.out.println(s);
} 

You can add any other keyword (just add it to regEx) without changing code!

您可以添加任何其他关键字(只需将其添加到regEx)而无需更改代码!

#3


0  

Try out using the StringTokenizer to split the string as below:

尝试使用StringTokenizer分割字符串,如下所示:

final String SPLIT_STR = ",";
  String str = "{route=Head west on, some address, distance=0.2 km, duration=1 min}"; 
  final StringTokenizer stToken = new StringTokenizer(
     str, SPLIT_STR);
  final String[] splitStr = new String[stToken.countTokens()];
  int index = 0;
  while(stToken.hasMoreElements()) {
     splitStr[index++] = stToken.nextToken();
  }
  for(index=0; index < splitStr.length; index++) {
     System.out.println("Tokenizer : " + splitStr[index]);
  }

}

Hope this will work for you.

希望这对你有用。