打印这句话中的每个单词
Print each word from this sentence
我有下面这句话:
This is a text and we should print each word
我想打印这句话中的每个单词。
package lab2_3;
public class Main {
public static void main(String[] args) {
String s2 = "This is a text and we should print each word";
int i;
int j;
for (i = 0; i <= s2.length() - 1; i++){
if (s2.substring(i).startsWith(" ") || i == 0){
//here I search for the start of the sentence or " "
for (j = i + 1; j <= s2.length() - 1; j++){
if (s2.substring(j).startsWith(" ") || j == s2.length() - 1) {
//here I search for the next " " or the end of the sentence
System.out.println(s2.substring(i, j));
//printing
i = j;
//i=j because the next search must be done from where we left
}
}
}
}
}
}
输出:
This
is
a
text
and
we
should
print
each
wor
如您所见,它几乎可以工作,但最后一个单词中缺少字母 d。
一个可能的解决方案是在末尾添加“”,它会起作用,但我不想那样做。
你能告诉我我的错误在哪里以及如何解决吗?
另外,能否请您提供一个更好的解决方案。
您可以拆分数组中的字符串
String s2 = "This is a text and we should print each word";
String [] s3 = s2.split(" ");
int i;
for (i = 0; i < s3.length; i++)
System.out.println(s3[i]);
}
如果您不想对 space 进行硬编码,您可以使用正则表达式
String s2 = "This is a text and we should print each word";
String [] s3 = s2.split("\s");
int i;
for (i = 0; i < s3.length; i++)
System.out.println(s3[i]);
}
输出
This
is
a
text
and
we
should
print
each
word
- 声明
String
- 使用
String
对象上的 split()
方法将字符串拆分为数组
- 遍历数组中的每个元素并将其打印到屏幕
public static void main(String[] args) {
String slashString = "This is a text and we should print each word";
for(String word : slashString.split(" ")){
System.out.println(word);
}
}
输出:
This
is
a
text
and
we
should
print
each
word
你把事情复杂化了。字符串已经有 split(regexDelimiter)
方法,它接受表示要拆分的位置的正则表达式。
另外 enhanced for loop 允许我们轻松迭代数组的所有元素或 Iterable 接口的实现
for (String str : strArray){
//do something with str
}
从 Java 8 开始,我们还有 String.join(delimiter, elements)
方法可以创建表示 element0[delimiter]element1[delimiter]..
.
的字符串
因此,根据您要查找的内容,您可能需要使用
for (String word : s2.split(" ")){
System.out.println(word);
}
或
String inEachLine = String.join(System.lineSeparator(), s2.split(" "));
或者更简单
String inEachLine = s2.replace(" ", System.lineSeparator());
最后一个例子只是在原始字符串的基础上创建了新的字符串,这个新字符串将用 OS 相关的行分隔符替换每个 space(例如 Windows \r\n
).
您还可以使用额外的 class 来帮助我们从字符串中读取数据。这个class是Scanner
。所以在你的情况下你可以简单地使用
Scanner sc = new Scanner(s2);
while(sc.hasNext()){
System.out.println(sc.next());
}
试试这个。
String s2 = "This is a text and we should print each word";
String[] t = s2.split("(?= )");
for (String e : t)
System.out.println(e);
结果
This
is
a
text
and
we
should
print
each
word
我有下面这句话:
This is a text and we should print each word
我想打印这句话中的每个单词。
package lab2_3;
public class Main {
public static void main(String[] args) {
String s2 = "This is a text and we should print each word";
int i;
int j;
for (i = 0; i <= s2.length() - 1; i++){
if (s2.substring(i).startsWith(" ") || i == 0){
//here I search for the start of the sentence or " "
for (j = i + 1; j <= s2.length() - 1; j++){
if (s2.substring(j).startsWith(" ") || j == s2.length() - 1) {
//here I search for the next " " or the end of the sentence
System.out.println(s2.substring(i, j));
//printing
i = j;
//i=j because the next search must be done from where we left
}
}
}
}
}
}
输出:
This
is
a
text
and
we
should
print
each
wor
如您所见,它几乎可以工作,但最后一个单词中缺少字母 d。 一个可能的解决方案是在末尾添加“”,它会起作用,但我不想那样做。
你能告诉我我的错误在哪里以及如何解决吗?
另外,能否请您提供一个更好的解决方案。
您可以拆分数组中的字符串
String s2 = "This is a text and we should print each word";
String [] s3 = s2.split(" ");
int i;
for (i = 0; i < s3.length; i++)
System.out.println(s3[i]);
}
如果您不想对 space 进行硬编码,您可以使用正则表达式
String s2 = "This is a text and we should print each word";
String [] s3 = s2.split("\s");
int i;
for (i = 0; i < s3.length; i++)
System.out.println(s3[i]);
}
输出
This
is
a
text
and
we
should
print
each
word
- 声明
String
- 使用
String
对象上的split()
方法将字符串拆分为数组 - 遍历数组中的每个元素并将其打印到屏幕
public static void main(String[] args) {
String slashString = "This is a text and we should print each word";
for(String word : slashString.split(" ")){
System.out.println(word);
}
}
输出:
This
is
a
text
and
we
should
print
each
word
你把事情复杂化了。字符串已经有 split(regexDelimiter)
方法,它接受表示要拆分的位置的正则表达式。
另外 enhanced for loop 允许我们轻松迭代数组的所有元素或 Iterable 接口的实现
for (String str : strArray){
//do something with str
}
从 Java 8 开始,我们还有 String.join(delimiter, elements)
方法可以创建表示 element0[delimiter]element1[delimiter]..
.
因此,根据您要查找的内容,您可能需要使用
for (String word : s2.split(" ")){
System.out.println(word);
}
或
String inEachLine = String.join(System.lineSeparator(), s2.split(" "));
或者更简单
String inEachLine = s2.replace(" ", System.lineSeparator());
最后一个例子只是在原始字符串的基础上创建了新的字符串,这个新字符串将用 OS 相关的行分隔符替换每个 space(例如 Windows \r\n
).
您还可以使用额外的 class 来帮助我们从字符串中读取数据。这个class是Scanner
。所以在你的情况下你可以简单地使用
Scanner sc = new Scanner(s2);
while(sc.hasNext()){
System.out.println(sc.next());
}
试试这个。
String s2 = "This is a text and we should print each word";
String[] t = s2.split("(?= )");
for (String e : t)
System.out.println(e);
结果
This
is
a
text
and
we
should
print
each
word