3

Basically I have these connection strings that I need to split up, heres an example of one:

name=type,info1;101;localhost;-1;false;false

I want to split it up into three variables: name, type and info.

the name is the bit before the '=' ("name") the type is the bit after the '=' and before the ',' ("type") the info is everything after the ',' ("info1;101;localhost;-1;false;false")

I have tried using the ".split" function but to no avail. Could anybody help me do it using a regular expression with substrings? Thanks.

Not had much practise with the split function, so it went like this:

String name [] = connString.split(",");
String type [] = connString.split(";");
String info [] = connString.split("");

MORE:

could you use the '.split' method to split up the parameters in this line from an XML doc?

 <rect x="298.43" width="340.00" y="131.12" height="380.00" id="rect_1" style="stroke-width: 1; stroke: rgb(0, 0, 0); fill: rgb(255, 102, 0); "/>
4
  • 4
    Can you post your split()-related code ? That should normally work. Bear in mind that split() takes a regular expression Commented Aug 23, 2012 at 8:27
  • You can refer any regex tutorials. it's very simple! Commented Aug 23, 2012 at 8:27
  • 1
    Please post Short, Self contained and Correct Code to get proper , possibly a correct answer. Commented Aug 23, 2012 at 8:30
  • What happens when you use split method? Commented Aug 23, 2012 at 8:34

5 Answers 5

6

Do you mean?

String s = "name=type,info1;101;localhost;-1;false;false";
String[] parts = s.split(",");
String[] parts2 = parts[0].split("=");
String name = parts2[0];
String type = parts2[1];
String info = parts[1];
Sign up to request clarification or add additional context in comments.

Comments

4

Using only one .split():

String s = "name=type,info1;101;localhost;-1;false;false";
String[] words = s.split("=|,");
String name = words[0];
String type = words[1];
String info = words[2];
System.out.println("Name: " + name + "\nType: " + type + "\nInfo: " + info);

Output:

Name: name
Type: type
Info: info1;101;localhost;-1;false;false

Comments

4

I think that you should use patterns here.

Pattern p = Pattern.compile("(\\w+)=(\\w+),(.*)");
Matcher m = p.matcher(str);
if (m.find()) {
    String name = m.group(1);
    String type = m.group(2);
    String info = m.group(3);
}

Comments

1

Split:

@Test
public void testParseUsingSplit() {
    String line = "name=type,info1;101;localhost;-1;false;false";

    String name;
    String type;
    String info;

    String[] split1 = line.split(",", 2);
    info = split1[1];
    String[] split2 = split1[0].split("=");
    name = split2[0];
    type = split2[1];

    Assert.assertEquals("name", name);
    Assert.assertEquals("type", type);
    Assert.assertEquals("info1;101;localhost;-1;false;false", info);
}

Regex:

@Test
public void testParseUsingRegex() {
    String line = "name=type,info1;101;localhost;-1;false;false";

    Pattern pattern = Pattern.compile("([^=]+)=([^,]+),(.*)");
    Matcher m = pattern.matcher(line);
    Assert.assertTrue(m.matches());

    String name = m.group(1);
    String type = m.group(2);
    String info = m.group(3);

    Assert.assertEquals("name", name);
    Assert.assertEquals("type", type);
    Assert.assertEquals("info1;101;localhost;-1;false;false", info);
}

Comments

1
public  void splitString(String connectionString) {
        String[] splitted = connectionString.split(",");
        String[] nameAndType = splitted[0].split("=");
        String name = nameAndType[0];
        String type = nameAndType[1];
        String info = splitted[1].substring(splitted[1].indexOf("info")+4);
        System.out.println(" name "+name);
        System.out.println(" type "+type);
        System.out.println(" info "+info);
    }

Try this out. Is this what you are trying to do ?

2 Comments

you assume that info is actually always a String "info", not good.
yeah. Some interesting thought. :-) there is no need for that subs tring operation here. I admit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.