How to split a string into specific conditions

I have the following line

books / a / grayL88 / WilliamsMC88: M. Howard Williams :: PA Massey :: Jim A. Crummond: Benchmarking Prolog for Database Applications.

How should I use or, in other words, what to use so that, finally, I can get

M. Howard Williams - Trial Test for Database Applications.

R. A. Massey - Trial Protocol for Database Applications.

Jim A. Crummond - Trial Protocol for Database Applications.

thanks

+6
source share
5 answers

First way:

  • You can first break your line based on ::: . This will give you an array of length 3. You will be interested in the 2nd and 3rd element of your array.
  • Then divide the 2nd element your array by :: . This will give you an array containing each name .
  • Go through the 2nd array and type each name with the 3rd element your first array.

     String str = "books/eh/grayL88/WilliamsMC88:::M. Howard Williams::" + "PA Massey::Jim A. Crammond:::Benchmarking Prolog for " + "Database Applications."; String[] arr = str.split(":::"); String[] innerArr = arr[1].split("::"); for (String name: innerArr) { System.out.println(name + " -- " + arr[2]); } 

OUTPUT : -

M. Howard Williams - Trial Test for Database Applications.
PA Massey - Benchmarking Prolog for Database Applications.

Jim A. Crummond - Trial Protocol for Database Applications.


The second way:

Or can you divide by :::? . This will be split into :: or ::: , which will receive every single element only in your first array (will only work for names 3 For more, you better use the first)

  String[] arr = str.split(":::?"); System.out.println(arr[1] + " - " + arr[4]); System.out.println(arr[2] + " - " + arr[4]); System.out.println(arr[3] + " - " + arr[4]); 

OUTPUT : -

M. Howard Williams - Benchmarking Prolog for Database Applications. A. Massey - Trial protocol for database applications.
Jim A. Crammond - Trial Protocol for Database Applications.

+2
source

You can try something like this:

 String s = "books/eh/grayL88/WilliamsMC88:::M. Howard Williams::PA Massey::Jim A. Crammond:::Benchmarking Prolog for Database Applications."; String split[] = s.split(":::"); String end = split[split.length - 1]; String[] names = split[1].split("::"); for (String name : names) System.out.println(name + " -- " + end); 

Conclusion:

M. Howard Williams - Benchmarking Prolog for Database Applications.
PA Massey - Trial test for database applications.
Jim A. Krummond (Benchmarking Prolog) for database applications.

+2
source

Try something like: (Pseudocode ...)

 myString.remove("(.*/)+[^:]*:{3}"); lastString = myString.split(":::").takeLast(); array names = myString.split("([^:]*)+"); result = ""; for (i=0;i<name.size();result+=names[i++]+" -- "+lastString+"\n"); // do whatever with "result"... 

No freebies, never ...

+1
source

Use StringTokenizer. For instance:

 StringTokenizer tokenizer = new StringTokenizer(str, "://"); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); // ... and do whatever you like with the token } 
0
source
 String str = "books/eh/grayL88/WilliamsMC88:::M. Howard Williams::PA Massey::" + "Jim A. Crammond:::Benchmarking Prolog for Database Applications."; String split[]=str.split(":::"); String names[] = split[1].split("::"); String sentence = split[2]; for(String name : names){ System.out.println(name + " -- " +sentence); } 

Exit:

M. Howard Williams - Benchmarking Prolog for Database Applications.
PA Massey - Trial test for database applications.
Jim A. Krummond (Benchmarking Prolog) for database applications.

0
source

All Articles