-1

I am reading in lines,and I need to split each line when the delimiter ; (semicolon + space) is present. Each line is as follows: s1; s2 s3; s4. I want to split this line where ; (semicolon + space) exists. I want to split only where semicolon and space are adjacent to each other, where a space follows a semicolon.

How would I do this using StringTokenizer?

I have already tried split("; ") but this splits the String at every occurrence of a space.

I have also tried new StringTokenizer(strLine,"\s*;\s+"), but this splits at every occurrence of 's'.

String[] parsedStr = inputString.split(...);

StringTokenizer st = new StringTokenizer(...);

I expect the output to be:

("s1","s2 s3","s4") Every token should be an element within this array.

NOT

("s1"," s2 s3"," s4") 

NOR

("s1","s2","s3","s4")
Danny
  • 77
  • 1
  • 8

3 Answers3

0

You can use regex with "\\s*;\\s+". It will remove any initial or trailing spaces. Also requires ';' to be followed with at least 1 space.

        String inputString = "s1; s2 s3; s4;s5";
        String[] parsedStr = inputString.split("\\s*;\\s+");

        System.out.print(Arrays.asList(parsedStr)); // ["s1", "s2 s3", "s4;s5"]

Edit:

If you need to split by semicolon and exact one space and keep the other spaces, you can modify the above solution as follows:

        String inputString = "s1; s2 s3; s4;s5; s6;  s7;";
        String[] parsedStr = inputString.split(";\\s{1}");

        System.out.print(Arrays.asList(parsedStr)); // ["s1", "s2 s3", "s4;s5", "s6", " s7;"]
MBD
  • 1
  • 1
  • 1
  • I tried the code, but get "s4" and "s5" as two strings, not "s4;s5" – Blangero May 03 '19 at 05:46
  • Sorry, I fixed it. You need \\s+ (at least one space) instead of \\s* (0 or more) after ';' – MBD May 03 '19 at 05:48
  • Yes, now "s4;s5" shows. but if the input is "s1; s2 s3; s4;s5";( two space in front of s2), then other issue raises. maybe this is not a good direction for solving the problem : - ) – Blangero May 03 '19 at 05:50
  • no, I mean when there are two spaces in front of "s2 s3", then the result should be " s2 s3" ( with one space in front of s2). but \\s* removes all the spaces. – Blangero May 03 '19 at 05:58
  • Then you should specify the exact number of the trailing spaces, like that ";\\s{1}". I will update my answer. – MBD May 03 '19 at 06:01
  • I tried this using a StringTokenizer, st = new StringTokenizer(strLine, "\\s*;\\s+"); but it gives me a new issue, spliting at every occurrence of "s". – Danny May 03 '19 at 15:55
0

As per your previous question, the solution would be as below :

   public class Split {
    public static void main(String[] args) {
        String inputString = "s1; s2 s3; s4;s5";
        String[] parsedStr = inputString.split("; | |;");
        System.out.print(Arrays.asList(parsedStr));
    } }

Since, you have edited your problem statement, the solution will be simple like :

inputString.split("; ");
Utkarsh Bhatt
  • 143
  • 5
  • 10
-1
public static void main(String [] args){
 java.util.Scanner input=new java.util.Scanner
                        ("s1; s2 s3; s4").useDelimiter("\\s*; \\s*");

       while(input.hasNext())
       {
       System.out.print(input.next()+",");
       }
  input.close();
}
Liam Wilson
  • 127
  • 7