在 java 中的特定位置拆分字符串
Splitting a string at a particular position in java
假设您有一个 "word1 word2 word3 word4"
形式的字符串。拆分它的最简单方法是 split[0] = "word1 word2"
和 split[1] = "word3 word4"
?
编辑:澄清
我想这样拆分,而不是拆分 [0] = "word1",我有前 2 个单词(我同意它不清楚)和拆分 [1] 中的所有其他单词,即在第二个 space
我会使用 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
这将导致 a = "word1 word2" 和 b = "word3 word4"
String str = "word1 word2 word3 word4";
String subStr1 = str.substring(0,12);
String subStr2 = str.substring(12);
将是您在某个位置拆分的最佳选择。如果您需要在第二次出现 space 时拆分,for 循环可能是更好的选择。
int count = 0;
int splitIndex;
for (int i = 0; i < str.length(); i++){
if(str.charAt(i) == " "){
count++;
}
if (count == 2){
splitIndex = i;
}
}
然后你会把它拆分成上面的子字符串。
您想一直成对进行吗?这是SO社区wiki提供的动态解决方案,Extracting pairs of words using String.split()
String input = "word1 word2 word3 word4";
String[] pairs = input.split("(?<!\G\w+)\s");
System.out.println(Arrays.toString(pairs));
输出:
[word1 word2, word3 word4]
这应该可以达到您想要的效果。
您可以使用 String.split(" ");
拆分初始字符串中的空格。
然后从那里你说你想要 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] + " ";
}
}
如果您想将一个字符串拆分为两个单词的集合,此代码可能会有所帮助:
String toBeSplit = "word1 word2 word3 word4";
String firstSplit = a.substr(0,tBS.indexOf(" ", tBS.indexOf(" ")));
String secondSplit = firstSplit.substr(tBS.indexOf(" ", tBS.indexOf(" ")));
按字符串的公共分隔符(在本例中为空格)拆分字符串,并有条件地在输出中重新添加选择的分隔符,迭代 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]
}
假设您有一个 "word1 word2 word3 word4"
形式的字符串。拆分它的最简单方法是 split[0] = "word1 word2"
和 split[1] = "word3 word4"
?
编辑:澄清
我想这样拆分,而不是拆分 [0] = "word1",我有前 2 个单词(我同意它不清楚)和拆分 [1] 中的所有其他单词,即在第二个 space
我会使用 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
这将导致 a = "word1 word2" 和 b = "word3 word4"
String str = "word1 word2 word3 word4";
String subStr1 = str.substring(0,12);
String subStr2 = str.substring(12);
将是您在某个位置拆分的最佳选择。如果您需要在第二次出现 space 时拆分,for 循环可能是更好的选择。
int count = 0;
int splitIndex;
for (int i = 0; i < str.length(); i++){
if(str.charAt(i) == " "){
count++;
}
if (count == 2){
splitIndex = i;
}
}
然后你会把它拆分成上面的子字符串。
您想一直成对进行吗?这是SO社区wiki提供的动态解决方案,Extracting pairs of words using String.split()
String input = "word1 word2 word3 word4";
String[] pairs = input.split("(?<!\G\w+)\s");
System.out.println(Arrays.toString(pairs));
输出:
[word1 word2, word3 word4]
这应该可以达到您想要的效果。
您可以使用 String.split(" ");
拆分初始字符串中的空格。
然后从那里你说你想要 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] + " ";
}
}
如果您想将一个字符串拆分为两个单词的集合,此代码可能会有所帮助:
String toBeSplit = "word1 word2 word3 word4";
String firstSplit = a.substr(0,tBS.indexOf(" ", tBS.indexOf(" ")));
String secondSplit = firstSplit.substr(tBS.indexOf(" ", tBS.indexOf(" ")));
按字符串的公共分隔符(在本例中为空格)拆分字符串,并有条件地在输出中重新添加选择的分隔符,迭代 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]
}