java 在java中的特定位置拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28419586/
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
Splitting a string at a particular position in java
提问by giulio
Assume you have a string of the form "word1 word2 word3 word4"
. What is the simplest way to split it such that split[0] = "word1 word2"
and split[1] = "word3 word4"
?
假设您有一个形式为 的字符串"word1 word2 word3 word4"
。什么是分裂它使得简单的方法split[0] = "word1 word2"
和split[1] = "word3 word4"
?
EDIT: Clarifying
编辑:澄清
I want to split such that instead of having split[0] = "word1", I have the first 2 words (I agree it was not clear) and all the other words in split[1], ie on the second space
我想拆分,而不是 split[0] = "word1",我有前 2 个词(我同意它不清楚)和 split[1] 中的所有其他词,即在第二个空格
回答by Einar Sundgren
I would use the String.substring(beginIndex, endIndex); and String.substring(beginIndex);
我会使用 String.substring(beginIndex, endIndex); 和 String.substring(beginIndex);
String a = "word1 word2 word3 word4";
int first = a.indexOf(" ");
int second = a.indexOf(" ", first + 1);
String b = a.substring(0,second);
String c = b.subString(second); // Only startindex, cuts at the end of the string
This would result in a = "word1 word2" and b = "word3 word4"
这将导致 a = "word1 word2" 和 b = "word3 word4"
回答by ThisClark
Do you want to do this in pairs always? This is a dynamic solution provided by the SO community wiki, Extracting pairs of words using String.split()
你想总是成对地这样做吗?这是 SO 社区维基提供的动态解决方案,使用 String.split() 提取单词对
String input = "word1 word2 word3 word4";
String[] pairs = input.split("(?<!\G\w+)\s");
System.out.println(Arrays.toString(pairs));
output:
输出:
[word1 word2, word3 word4]
回答by TeneCursum
String str = "word1 word2 word3 word4";
String subStr1 = str.substring(0,12);
String subStr2 = str.substring(12);
Would be your best bet for splitting at a position. If you need to split on the second occurrence of space, a for loop might be a better option.
将是您在某个位置拆分的最佳选择。如果您需要在第二次出现空间时拆分,for 循环可能是更好的选择。
int count = 0;
int splitIndex;
for (int i = 0; i < str.length(); i++){
if(str.charAt(i) == " "){
count++;
}
if (count == 2){
splitIndex = i;
}
}
and then you would split it into substrings as above.
然后你会像上面那样将它拆分成子串。
回答by austin wernli
This should do what you want to achieve.
这应该做你想要实现的目标。
You can use String.split(" ");
to split on spaces in the initial string.
您可以使用String.split(" ");
在初始字符串中的空格处进行拆分。
Then from there you said you wanted the first two words in split[0]
so i just handled that with a simple conditional if(i==0 || i == 1) add it to split[0]
然后从那里你说你想要前两个词,split[0]
所以我只是用一个简单的条件处理if(i==0 || i == 1) add it to split[0]
String word = "word1 word2 word3 word4";
String[] split = new String[2];
//Split the initial string on spaces which will give you an array of the words.
String[] wordSplit = word.split(" ");
//Foreach item wordSplit add it to either Split[0] or Split[1]
for (int i = 0; i < wordSplit.length(); i++) {
//Determine which position to add the string to
if (i == 0 || i == 1) split[0] += wordSplit[i] + " ";
else {
split[1] += wordSplit[i] + " ";
}
}
回答by Russel Peterson
If you would want to split a string into sets of two words, this code could help:
如果您想将字符串拆分为两个单词的集合,此代码可能会有所帮助:
String toBeSplit = "word1 word2 word3 word4";
String firstSplit = a.substr(0,tBS.indexOf(" ", tBS.indexOf(" ")));
String secondSplit = firstSplit.substr(tBS.indexOf(" ", tBS.indexOf(" ")));
回答by sammarcow
Split the string by its common delimiter (spaces in this case) and conditionally re-add the delimiter of choice in output, iterating by 2
通过其公共分隔符(在本例中为空格)拆分字符串,并有条件地在输出中重新添加选择的分隔符,迭代 2
string original = "word1 word2 word3 word4";
string[] delimitedSplit = original.split(" ");
for (int i = 0; i< delimitedSplit.length; i+=2) {
if (i < delimitedSplit.length - 1 ) { //handle uneven pairs
out.println(delimitedSplit[i] + " " + delimitedSplit[i+1] );
}
else {
out.println(delimitedSplit[i]
}