-4

I have a little problem with splitting a String

String anl_gewerk = "Text Text Text (KG 412/2)"
String[] parts = anl_gewerk.split("[(]");
anl_gewerk = parts[1];
anl_gewerk = anl_gewerk.replaceAll("\\(","").replaceAll("\\)","");
anl_gewerk = anl_gewerk.replace("KG","");

I have the aforementioned string, and I'm searching for "412/2".
Therefore, I want to split the String into two substrings searching for "(".
Finally I want to grab this String deleting "(", "KG", " " and ")".

When I select anl_gewerk = parts[0]; it works but I get the wrong part, when I change into parts[1] the App crashes.

Please help me

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    https://stackoverflow.com/a/12595052/1848157 , refer this . – Radhey Jun 15 '17 at 11:27
  • Next step is , you need to split that newly string using #space , " " . – Radhey Jun 15 '17 at 11:28
  • try this: String anl_gewerk = "Text Text Text (KG 412/2)" String[] parts = anl_gewerk.split("("); String anl_gewerk1 = parts[1]; String[] parts2= anl_gewerk1.split(" "); String anl_gewerk2 = parts2[1]; String[] parts3=anl_gewerk2.split(")"); String finalString=parts3[0]; – Divyesh Patel Jun 15 '17 at 11:29
  • 2
    Voted to close as not reproducible: http://ideone.com/6vyy6m – Tom Jun 15 '17 at 11:30
  • This is working fine `String anl_gewerk = "Text Text Text (KG 412/2)"; String[] parts = anl_gewerk.split("[(]"); anl_gewerk = parts[1]; anl_gewerk = anl_gewerk.replaceAll("\\(","").replaceAll("\\)",""); anl_gewerk = anl_gewerk.replace("KG","").trim();` – Jinesh Francis Jun 15 '17 at 11:37
  • Possible duplicate of [How to get a string between two characters?](https://stackoverflow.com/questions/12595019/how-to-get-a-string-between-two-characters) – Yep_It's_Me Jun 15 '17 at 13:18
  • Thanks for all the answer. I have tried every answer, but it dosen´t work... – David Loraj Jun 15 '17 at 14:20
  • Is it maybe because of the "/"? – David Loraj Jun 15 '17 at 14:20

1 Answers1

0

Try to change your code by this:

    String anl_gewerk = "Text Text Text (KG 412/2)";
    String[] parts = anl_gewerk.split("[(]");
    anl_gewerk = parts[1];
    String sub[] = anl_gewerk.split(" ");
    String test = sub[1];
    String result = test.replace(")","");// it gives your result 412/2