Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.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_Split - Fatal编程技术网

Java 如何拆分此所需格式的字符串?

Java 如何拆分此所需格式的字符串?,java,string,split,Java,String,Split,我有下面的文本格式 String s = " key1:value1; key2:value2; key3:value3; key4:value4; key5:value5; key6:https://url1.com, https://url2.com; key7:value;"; Note: (the number of urls in key6 will be 1 to many and non linear) 我使用s.split(“;”)将s拆分为7个键值块 现在我

我有下面的文本格式

 String s = "
 key1:value1;
 key2:value2;
 key3:value3;
 key4:value4;
 key5:value5;
 key6:https://url1.com, https://url2.com;
 key7:value;";
 Note: (the number of urls in key6 will be 1 to many and non linear)
我使用s.split(“;”)将s拆分为7个键值块

现在我想再次分别拆分键和值,并将其存储在数组0和ist位置中

     //while looping into keyValPair[i]
     String[] singleKeyVal[] = keyValPair[0].split(":");
     /*Output
         singleKeyVal[0] will have Key1
         singleKeyVal[1] will have Value1
          perform some task and clear the array singlekeyVal[] 
问题是如何正确拆分键6

 //while looping into KeyValPair[i]
 String[] singleKeyVal[] = keyValPair[5].split(":");  //6th chunk contains : in the URL too
 /*Output
     singleKeyVal[0] will have Key6
     singleKeyVal[1] should contain https://url1.com,https://url2.com
     also note that above example contains only 2 urls but it will contain urls between 1 to many urls,
第二个参数是
limit

limit参数控制应用阵列的次数,因此会影响结果阵列的长度

所以使用

String[] SingleKeyVal[] = KeyValPair[5].split(":", 2);
要仅拆分一次并获得大小为2的数组,需要使用第二个参数
limit

limit参数控制应用阵列的次数,因此会影响结果阵列的长度

所以使用

String[] SingleKeyVal[] = KeyValPair[5].split(":", 2);

只拆分一次并获得大小为2的数组。

有两种
split
方法-使用
limit
参数指定所需的最大组数。就你而言:

String[] singleKeyVal = keyValPair[5].split(":", 2);
你应该做你想做的


ps:您应该采用Java命名约定(变量以小写开头)。

有两种
split
方法-采用
limit
参数,允许您指定所需的最大组数。就你而言:

String[] singleKeyVal = keyValPair[5].split(":", 2);
你应该做你想做的


ps:您应该采用Java命名约定(变量以小写开头)。

不使用indexOf(:“”)进行拆分,而是使用索引中的子字符串。不使用indexOf(:“”)进行拆分,而是使用索引中的子字符串。