复制 java 中字符串的前 N 个单词
Copy the first N words in a string in java
我想select一个文本字符串的前N个词。
我试过 split()
和 substring()
都无济于事。
我要的是select下面祷告的前3个字复制到另一个变量中
例如,如果我有一个字符串:
String greeting = "Hello this is just an example"
我想进入变量 Z 的前 3 个词,这样
Z = "Hello this is"
您可以尝试类似的方法:
String greeting = "Hello this is just an example";
int end = 0;
for (int i=0; i<3; i++) {
end = greeting.indexOf(' ', end) + 1;
}
String Z = greeting.substring(0, end - 1);
N.B。这假设源字符串中至少有三个 space 字符。再少一点,这段代码可能会失败。
public String getFirstNStrings(String str, int n) {
String[] sArr = str.split(" ");
String firstStrs = "";
for(int i = 0; i < n; i++)
firstStrs += sArr[i] + " ";
return firstStrs.trim();
}
现在 getFirstNStrings("Hello this is just an example", 3);
将输出:
Hello this is
String myString = "Copying first N numbers of words to a string";
String [] arr = myString.split("\s+");
//Splits words & assign to the arr[] ex : arr[0] -> Copying ,arr[1] -> first
int N=3; // NUMBER OF WORDS THAT YOU NEED
String nWords="";
// concatenating number of words that you required
for(int i=0; i<N ; i++){
nWords = nWords + " " + arr[i] ;
}
System.out.println(nWords);
注意:这里 .split() 函数 returns 一个字符串数组,通过围绕给定正则表达式的 匹配项拆分给定字符串来计算
所以如果我编写如下代码
String myString = "1234M567M98723651";
String[] arr = myString.split("M"); //idea : split the words if 'M' presents
那么答案将是:
1234 和 567 存储在一个数组中。
这是通过将拆分值存储到给定数组中来实现的。第一个拆分值存储到 arr[0],第二个到 arr[1].
代码的后半部分用于连接所需数量的拆分词
希望您能从中得到启发!!!
谢谢!
将此添加到实用程序 class 中,例如 Util.java
public static String getFirstNWords(String s, int n) {
if (s == null) return null;
String [] sArr = s.split("\s+");
if (n >= sArr.length)
return s;
String firstN = "";
for (int i=0; i<n-1; i++) {
firstN += sArr[i] + " ";
}
firstN += sArr[n-1];
return firstN;
}
用法:
Util.getFirstNWords("This will give you the first N words", 3);
---->
"This will give"
如果你使用 Apache Commons Lang3,你可以像这样让它更短一些:
public String firstNWords(String input, int numOfWords) {
String[] tokens = input.split(" ");
tokens = ArrayUtils.subarray(tokens, 0, numOfWords);
return StringUtils.join(tokens, ' ');
}
大多数发布的答案已经使用正则表达式,如果我们必须处理大量字符串,这可能会成为一种开销。甚至 str.split(" ")
在内部使用正则表达式操作。 dave 的答案可能是最有效的,但它不能正确处理同时出现多个 space 的字符串,除了假设常规 space 是唯一的单词分隔符并且输入字符串有 3 个或更多话(他已经提出的假设)。如果在一个选项中使用 Apache Commons,那么我会使用以下代码,因为它不仅简洁而且甚至在内部避免使用正则表达式,而且还可以优雅地处理少于 3 个单词的输入字符串:
/* Splits by whitespace characters. All characters after the 3rd whitespace,
* if present in the input string, go into the 4th "word", which could really
* be a concanetation of multiple words. For the example in the question, the
* 4th "word" in the result array would be "just an example". Invoking the
* utility method with max-splits specified is slightly more efficient as it
* avoids the need to look for and split by space after the first 3 words have
* been extracted
*/
String[] words = StringUtils.split(greeting, null, 4);
String Z = StringUtils.join((String[]) ArrayUtils.subarray(words, 0, 3), ' ');
我想select一个文本字符串的前N个词。
我试过 split()
和 substring()
都无济于事。
我要的是select下面祷告的前3个字复制到另一个变量中
例如,如果我有一个字符串:
String greeting = "Hello this is just an example"
我想进入变量 Z 的前 3 个词,这样
Z = "Hello this is"
您可以尝试类似的方法:
String greeting = "Hello this is just an example";
int end = 0;
for (int i=0; i<3; i++) {
end = greeting.indexOf(' ', end) + 1;
}
String Z = greeting.substring(0, end - 1);
N.B。这假设源字符串中至少有三个 space 字符。再少一点,这段代码可能会失败。
public String getFirstNStrings(String str, int n) {
String[] sArr = str.split(" ");
String firstStrs = "";
for(int i = 0; i < n; i++)
firstStrs += sArr[i] + " ";
return firstStrs.trim();
}
现在 getFirstNStrings("Hello this is just an example", 3);
将输出:
Hello this is
String myString = "Copying first N numbers of words to a string";
String [] arr = myString.split("\s+");
//Splits words & assign to the arr[] ex : arr[0] -> Copying ,arr[1] -> first
int N=3; // NUMBER OF WORDS THAT YOU NEED
String nWords="";
// concatenating number of words that you required
for(int i=0; i<N ; i++){
nWords = nWords + " " + arr[i] ;
}
System.out.println(nWords);
注意:这里 .split() 函数 returns 一个字符串数组,通过围绕给定正则表达式的 匹配项拆分给定字符串来计算
所以如果我编写如下代码
String myString = "1234M567M98723651";
String[] arr = myString.split("M"); //idea : split the words if 'M' presents
那么答案将是:
1234 和 567 存储在一个数组中。
这是通过将拆分值存储到给定数组中来实现的。第一个拆分值存储到 arr[0],第二个到 arr[1].
代码的后半部分用于连接所需数量的拆分词
希望您能从中得到启发!!!
谢谢!
将此添加到实用程序 class 中,例如 Util.java
public static String getFirstNWords(String s, int n) {
if (s == null) return null;
String [] sArr = s.split("\s+");
if (n >= sArr.length)
return s;
String firstN = "";
for (int i=0; i<n-1; i++) {
firstN += sArr[i] + " ";
}
firstN += sArr[n-1];
return firstN;
}
用法: Util.getFirstNWords("This will give you the first N words", 3); ----> "This will give"
如果你使用 Apache Commons Lang3,你可以像这样让它更短一些:
public String firstNWords(String input, int numOfWords) {
String[] tokens = input.split(" ");
tokens = ArrayUtils.subarray(tokens, 0, numOfWords);
return StringUtils.join(tokens, ' ');
}
大多数发布的答案已经使用正则表达式,如果我们必须处理大量字符串,这可能会成为一种开销。甚至 str.split(" ")
在内部使用正则表达式操作。 dave 的答案可能是最有效的,但它不能正确处理同时出现多个 space 的字符串,除了假设常规 space 是唯一的单词分隔符并且输入字符串有 3 个或更多话(他已经提出的假设)。如果在一个选项中使用 Apache Commons,那么我会使用以下代码,因为它不仅简洁而且甚至在内部避免使用正则表达式,而且还可以优雅地处理少于 3 个单词的输入字符串:
/* Splits by whitespace characters. All characters after the 3rd whitespace,
* if present in the input string, go into the 4th "word", which could really
* be a concanetation of multiple words. For the example in the question, the
* 4th "word" in the result array would be "just an example". Invoking the
* utility method with max-splits specified is slightly more efficient as it
* avoids the need to look for and split by space after the first 3 words have
* been extracted
*/
String[] words = StringUtils.split(greeting, null, 4);
String Z = StringUtils.join((String[]) ArrayUtils.subarray(words, 0, 3), ' ');