Reputation: 190
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); "/>
Upvotes: 3
Views: 413
Reputation: 3288
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 ?
Upvotes: 1
Reputation: 10500
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
Upvotes: 4
Reputation: 115328
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);
}
Upvotes: 4
Reputation: 4289
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);
}
Upvotes: 1
Reputation: 533492
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];
Upvotes: 6