2012-08-23 16 views
3

基本上我有我需要拆分這些連接字符串,一個繼承人的一個例子:Java的子串

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

我想它分成三個變量:名稱,類型和信息。

名字是前位的「=」(「名稱」) 類型是在「=」的位和之前的「」(‘類型’) 的信息是以後的一切」, '(「info1; 101; localhost; -1; false; false」)

我試過使用「.split」函數,但無濟於事。任何人都可以幫助我使用帶有子字符串的正則表達式來執行此操作嗎?謝謝。

沒有與分流功能大量的練習,所以它是這樣的:

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

更多:

您可以使用「.split」的方法來分割參數在這條線從一個XML文檔?

<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

你可以發佈你的split()相關代碼嗎?這通常應該起作用。請記住,split()需要一個正則表達式 –

+0

您可以參考任何正則表達式教程。這很簡單! –

+1

請發佈Short,Self contained和Correct Code以獲取正確答案,可能是正確答案。 – sakthisundar

回答

4

僅使用一個.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); 

輸出:

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

你的意思是?

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]; 
1

斯普利特:

@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); 
} 

正則表達式:

@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); 
} 
4

我認爲你應該在這裏使用的模式。

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); 
} 
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); 
    } 

嘗試了這一點。這是你想要做的嗎?

+0

你認爲信息實際上總是一個字符串「信息」,不好。 – Eugene

+0

是的。一些有趣的想法。 :-)這裏沒有必要進行這種潛水作業。我承認。 – sakthisundar