-3

I have the following string:

String result = "@sequence@A:exampleA@B:exampleB";

I would like to split this string into two strings like this:

String resulta = "sequence";

String resultb = "@A:exampleA@B:exampleB";

How can I do this? I'm new to Java programming language.

Thanks!

reto
  • 9,995
  • 5
  • 53
  • 52
Kima321
  • 55
  • 1
  • 1
  • 8

3 Answers3

0

if you want typical split, (Specific to your string), you can call substring and get the part of it like below:

     String s = "@sequence@A:exampleA@B:exampleB";
        String s1= s.substring(1,s.indexOf("@",1));
        System.out.println(s1);
        String s2= s.substring(s1.length()+1);
        System.out.println(s2);
     }

Edit as per your comment

String s3= s.substring(s.indexOf("@",1));
System.out.println(s3);
Optional
  • 4,387
  • 4
  • 27
  • 45
  • Thanks very much :) But can you get "s2" only with main "s" variables? – Kima321 Oct 10 '17 at 07:16
  • @Kima321 Edited answer as per your comment using only s. In s1, we are doing substring till 2nd @. In s3, we are doing substring "from" 2nd @ – Optional Oct 10 '17 at 07:19
0
String result = "@sequence@A:exampleA@B:exampleB";

if(result.indexOf("@") == 0)
    result = result.substring(1, result.length());

String resultA = result.substring(0, result.indexOf("@"));
String resultB = result.substring(resultA.length(), result.length());
0

Try,

    String result = "@sequence@A:exampleA@B:exampleB", resulta, resultb;
    int splitPoint = result.indexOf('@',1);
    resulta = result.substring(1, splitPoint);
    resultb = result.substring(splitPoint);

    System.out.println(resulta+", "+resultb);
Damith
  • 417
  • 1
  • 5
  • 15