3

基本的に、分割する必要のあるこれらの接続文字列があります。その例を次に示します。

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

名前、タイプ、情報の3つの変数に分割したいと思います。

名前は「=」(「名前」)の前のビットですタイプは「=」の後のビットであり、「、」(「タイプ」)の前のビットです情報は「、」(「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

5 に答える 5

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];
于 2012-08-23T08:32:30.390 に答える
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);
}
于 2012-08-23T08:35:14.857 に答える
4

1つだけを使用する.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
于 2012-08-23T08:37:37.943 に答える
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);
}
于 2012-08-23T08:32:43.387 に答える
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);
    }

これを試してみてください。これはあなたがやろうとしていることですか?

于 2012-08-23T08:39:36.073 に答える