Sangram Anand
Sangram Anand

Reputation: 10844

Split between two chars in java

I want to split a string between two semicolons (:). i.e.,

BOOLEAN: Mr. Coffee - Recall:8a42bb8b36a6b0820136aa5e05dc01b3:1346790794980

I am trying

split("[\\:||\\:]");

but its not working

Upvotes: 0

Views: 268

Answers (4)

Vinesh
Vinesh

Reputation: 943

You can use split(), Refer this code,

String s ="BOOLEAN: Mr. Coffee - Recall:8a42bb8b36a6b0820136aa5e05dc01b3:1346790794980";

String temp = new String();


    String[] arr = s.split(":");

    for(String x : arr){
      System.out.println(x);
    }

Upvotes: 0

Owen
Owen

Reputation: 174

Regex flavoured:

    String yourString = "BOOLEAN: Mr. Coffee - Recall:8a42bb8b36a6b0820136aa5e05dc01b3:1346790794980";
    String [] componentStrings = Pattern.compile(":").split(yourString);

    for(int i=0;i<componentStrings.length;i++)
    {
        System.out.println(i + " - " + componentStrings[i]);
    }

Upvotes: 0

Averroes
Averroes

Reputation: 4228

This:

  String m = "BOOLEAN: Mr. Coffee - Recall:8a42bb8b36a6b0820136aa5e05dc01b3:1346790794980";
  for (String x : m.split(":"))
    System.out.println(x);

returns

BOOLEAN
 Mr. Coffee - Recall
8a42bb8b36a6b0820136aa5e05dc01b3
1346790794980

Upvotes: 0

Razvan
Razvan

Reputation: 10093

Use split with ":" as the regex.

More precisely:

  String splits[] = yourString.split(":");
  //splits will contain: 
  //splits[0] = "BOOLEAN";
  //splits[1] = "Mr. Coffee - Recall";
  //splits[2] = "8a42bb8b36a6b0820136aa5e05dc01b3";
  //splits[3] = "1346790794980";

Upvotes: 3

Related Questions