仅在第一个实例上拆分字符串 - java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18462826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
split string only on first instance - java
提问by dracula
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
我想用 '=' 字符分割一个字符串。但我希望它只在第一个实例中拆分。我怎样才能做到这一点 ?这是 '_' 字符的 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
谢谢
采纳答案by Zaheer Ahmed
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 nis 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.The string
boo:and:foo
, for example, yields the following results with these parameters: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" }
此方法返回的数组包含此字符串的每个子字符串,这些子字符串由与给定表达式匹配的另一个子字符串终止或由字符串的末尾终止。数组中的子字符串按它们在此字符串中出现的顺序排列。如果表达式不匹配输入的任何部分,则结果数组只有一个元素,即这个字符串。
该
limit
参数控制应用模式的次数,因此会影响结果数组的长度。如果限制n大于零,则该模式将最多应用n- 1 次,数组的长度将不大于n,并且数组的最后一个条目将包含最后一个匹配的分隔符之外的所有输入。
boo:and:foo
例如,string使用这些参数产生以下结果: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" }
回答by codeMan
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 文档参考:String#split(java.lang.String, int)
回答by Siva
As many other answers suggest the limit approach, This can be another way
正如许多其他答案所建议的限制方法一样,这可能是另一种方式
You can use the indexOfmethod on String which will returns the first Occurance of the given character, Using that index you can get the desired output
您可以在 String 上使用indexOf方法,该方法将返回给定字符的第一个出现次数,使用该索引可以获得所需的输出
String target = "apple=fruit table price=5" ;
int x= target.indexOf("=");
System.out.println(target.substring(x+1));
回答by Martin Ko??ál
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 :)
我喜欢编写自己的方法:)
回答by Kailash Karki
String string = "This is test string on web";
String splitData[] = string.split("\s", 2);
Result ::
splitData[0] => This
splitData[1] => is test string
String string = "This is test string on web";
String splitData[] = string.split("\s", 3);
Result ::
splitData[0] => This
splitData[1] => is
splitData[1] => test string on web
By default split method create n number's of arrays on the basis of given regex. But if you want to restrict number of arrays to create after a split than pass second argument as an integer argument.
默认情况下,split 方法根据给定的正则表达式创建 n 个数组。但是,如果您想限制拆分后要创建的数组数量,而不是将第二个参数作为整数参数传递。
回答by Bhola
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]);
}
}