muka90 muka90 - 5 months ago 12
Java Question

JAVA - Extract multiple parts from String

I have a dynamic String (from a SQL query) which looks like this

String x="abc_123_456_defgh_ijkl_mnop_qr"

First I separated them with substring(x,y) into different variables but now I figured out that the length of each part could vary.
Which means I need to separate the strings by character.

Goal is:

String x1=abc
String x2=123
String x3=456
String x4=defgh

I couldn't get regex to work...maybe because I'm quite new to JAVA :(
Thanks in advance!


You can use split method

    String x="abc_123_456_defgh_ijkl_mnop_qr";
    String parts[]=x.split("_");
    String x1=parts[0];
    String x2=parts[1];

You can check more info at