Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java子字符串_Java_String_Variables_Split_Substring - Fatal编程技术网

Java子字符串

Java子字符串,java,string,variables,split,substring,Java,String,Variables,Split,Substring,基本上我有这些需要拆分的连接字符串,下面是一个示例: name=type,info1;101;localhost;-1;false;false 我想把它分成三个变量:名称、类型和信息 名称是“=”前面的位(“名称”) 类型是“=”后面和“,”前面的位(“类型”) 信息是在“,”之后的所有内容(“info1;101;localhost;-1;false;false”) 我尝试过使用“.split”功能,但没有效果。有人能帮我用带子字符串的正则表达式吗?谢谢 没有太多实践分割函数,所以它是这样的:

基本上我有这些需要拆分的连接字符串,下面是一个示例:

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); "/>

你是说

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];
拆分:

正则表达式:

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

我认为你应该在这里使用模式

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

仅使用一个
.split()

输出:

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

试试这个。这就是您要做的吗?

您能发布与split()相关的代码吗?这应该正常工作。请记住,split()采用常规表达式。您可以参考任何正则表达式教程。这很简单!请发布简短、自包含且正确的代码以获得正确的答案。当您使用拆分方法时会发生什么?您假设信息实际上总是字符串“info”,这不好。是的。一些有趣的想法。:-)这里不需要那个substring操作。我承认。
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
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);
    }