仅在第一个实例上拆分字符串 - java

时间:2023-01-18 21:42:46

I want to split a string by '=' charecter. But I want it to split on first instance only. How can I do that ? Here is a JavaScript example for '_' char but it doesn't work for me split string only on first instance of specified character

我想用'='charecter分割一个字符串。但是我希望它只能在第一个实例中拆分。我怎样才能做到这一点 ?这是'_'char的JavaScript示例,但它不适用于仅在指定字符的第一个实例上拆分字符串

Example :

示例:

apple=fruit table price=5

When I try String.split('='); it gives

当我尝试String.split('=');它给

[apple],[fruit table price],[5]

But I need

但是我需要

[apple],[fruit table price=5]

Thanks

谢谢

6 个解决方案

#1


182  

string.split("=", 2);

As String.split(java.lang.String regex, int limit) explains:

正如String.split(java.lang.String regex,int limit)解释:

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

此方法返回的数组包含此字符串的每个子字符串,该子字符串由与给定表达式匹配的另一个子字符串终止,或者由字符串的结尾终止。数组中的子串按它们在此字符串中出现的顺序排列。如果表达式与输入的任何部分都不匹配,那么结果数组只有一个元素,即该字符串。

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.

limit参数控制模式的应用次数,因此会影响结果数组的长度。如果限制n大于零,那么模式将最多应用n - 1次,数组的长度将不大于n,并且数组的最后一个条目将包含超出最后一个匹配分隔符的所有输入。

The string boo:and:foo, for example, yields the following results with these parameters:

例如,字符串boo:和:foo使用以下参数产生以下结果:

Regex Limit    Result
:     2        { "boo", "and:foo" }
:     5        { "boo", "and", "foo" }
:    -2        { "boo", "and", "foo" }
o     5        { "b", "", ":and:f", "", "" }
o    -2        { "b", "", ":and:f", "", "" }
o     0        { "b", "", ":and:f" }

#2


7  

Yes you can, just pass the integer param to the split method

是的,你可以,只需将整数参数传递给split方法

String stSplit = "apple=fruit table price=5"

stSplit.split("=", 2);

Here is a java doc reference : String#split(java.lang.String, int)

这是一个java doc引用:String#split(java.lang.String,int)

#3


4  

As many other answers suggest the limit approach, This can be another way

正如许多其他答案提出的限制方法,这可能是另一种方式

You can use the indexOf method on String which will returns the first Occurance of the given character, Using that index you can get the desired output

您可以在String上使用indexOf方法,它将返回给定字符的第一个Occurance,使用该索引可以获得所需的输出

String target = "apple=fruit table price=5" ;
int x= target.indexOf("=");
System.out.println(target.substring(x+1));

#4


0  

String slpitString[] = stringInToSearch.split("pattern", 2);

String slpitString [] = stringInToSearch.split(“pattern”,2);

#5


0  

Try This Code...

试试这个代码......

Its Working.

它的工作。

public class Split
{
    public static void main(String...args)
    {
        String a = "%abcdef&Ghijk%xyz";
        String b[] = a.split("%", 2);

        System.out.println("Value = "+b[1]);
    }
}

#6


-2  

String[] func(String apple){
String[] tmp = new String[2];
for(int i=0;i<apple.length;i++){
   if(apple.charAt(i)=='='){
      tmp[0]=apple.substring(0,i);
      tmp[1]=apple.substring(i+1,apple.length);
      break;
   }
}
return tmp;
}
//returns string_ARRAY_!

i like writing own methods :)

我喜欢写自己的方法:)

#1


182  

string.split("=", 2);

As String.split(java.lang.String regex, int limit) explains:

正如String.split(java.lang.String regex,int limit)解释:

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

此方法返回的数组包含此字符串的每个子字符串,该子字符串由与给定表达式匹配的另一个子字符串终止,或者由字符串的结尾终止。数组中的子串按它们在此字符串中出现的顺序排列。如果表达式与输入的任何部分都不匹配,那么结果数组只有一个元素,即该字符串。

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.

limit参数控制模式的应用次数,因此会影响结果数组的长度。如果限制n大于零,那么模式将最多应用n - 1次,数组的长度将不大于n,并且数组的最后一个条目将包含超出最后一个匹配分隔符的所有输入。

The string boo:and:foo, for example, yields the following results with these parameters:

例如,字符串boo:和:foo使用以下参数产生以下结果:

Regex Limit    Result
:     2        { "boo", "and:foo" }
:     5        { "boo", "and", "foo" }
:    -2        { "boo", "and", "foo" }
o     5        { "b", "", ":and:f", "", "" }
o    -2        { "b", "", ":and:f", "", "" }
o     0        { "b", "", ":and:f" }

#2


7  

Yes you can, just pass the integer param to the split method

是的,你可以,只需将整数参数传递给split方法

String stSplit = "apple=fruit table price=5"

stSplit.split("=", 2);

Here is a java doc reference : String#split(java.lang.String, int)

这是一个java doc引用:String#split(java.lang.String,int)

#3


4  

As many other answers suggest the limit approach, This can be another way

正如许多其他答案提出的限制方法,这可能是另一种方式

You can use the indexOf method on String which will returns the first Occurance of the given character, Using that index you can get the desired output

您可以在String上使用indexOf方法,它将返回给定字符的第一个Occurance,使用该索引可以获得所需的输出

String target = "apple=fruit table price=5" ;
int x= target.indexOf("=");
System.out.println(target.substring(x+1));

#4


0  

String slpitString[] = stringInToSearch.split("pattern", 2);

String slpitString [] = stringInToSearch.split(“pattern”,2);

#5


0  

Try This Code...

试试这个代码......

Its Working.

它的工作。

public class Split
{
    public static void main(String...args)
    {
        String a = "%abcdef&Ghijk%xyz";
        String b[] = a.split("%", 2);

        System.out.println("Value = "+b[1]);
    }
}

#6


-2  

String[] func(String apple){
String[] tmp = new String[2];
for(int i=0;i<apple.length;i++){
   if(apple.charAt(i)=='='){
      tmp[0]=apple.substring(0,i);
      tmp[1]=apple.substring(i+1,apple.length);
      break;
   }
}
return tmp;
}
//returns string_ARRAY_!

i like writing own methods :)

我喜欢写自己的方法:)