如何写一个replaceAll函数java?
How to write a replaceAll function java?
我正在尝试编写一个程序,允许用户输入一个短语(例如:"I like cats"
)并在单独的行上打印每个单词。我已经编写了允许在每个 space 换行的部分,但我不想在 单词 之间有 空行 ] 因为 过剩 spaces。我不能使用任何 regular expressions
,例如 String.split()
、replaceAll()
或 trim()
。
我尝试使用几种不同的方法,但如果您不知道可能存在的确切数量,我不知道如何删除 space。我尝试了很多不同的 methods
但似乎没有任何效果。
有没有办法将它实现到我已经编写的代码中?
for (i=0; i<length-1;) {
j = text.indexOf(" ", i);
if (j==-1) {
j = text.length();
}
System.out.print("\n"+text.substring(i,j));
i = j+1;
}
或者如何为它写一个新的表达式?任何建议将不胜感激。
I have already written the part to allow a new line at every space but
I don't want to have blank lines between the words because of excess
spaces.
如果你不会用trim()
或replaceAll()
,你可以用java.util.Scanner
来读每个单词作为令牌。默认情况下 Scanner
使用白色 space 模式作为查找标记的分隔符。同样,您也可以使用 StringTokenizer
在新行上打印每个单词。
String str = "I like cats";
Scanner scanner = new Scanner(str);
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
输出
I
like
cats
像这样的东西呢,它的 O(N) 时间复杂度:
只需在遍历字符串时使用字符串生成器创建字符串,只要找到 space
添加“\n”
String word = "I like cats";
StringBuilder sb = new StringBuilder();
boolean newLine = true;
for(int i = 0; i < word.length(); i++) {
if (word.charAt(i) == ' ') {
if (newLine) {
sb.append("\n");
newLine = false;
}
} else {
newLine = true;
sb.append(word.charAt(i));
}
}
String result = sb.toString();
编辑:修复了评论中提到的问题(多个 space 换行)
这是一个使用 substring() 和 indexOf() 的简单解决方案
public static void main(String[] args) {
List<String> split = split("I like cats");
split.forEach(System.out::println);
}
public static List<String> split(String s){
List<String> list = new ArrayList<>();
while(s.contains(" ")){
int pos = s.indexOf(' ');
list.add(s.substring(0, pos));
s = s.substring(pos + 1);
}
list.add(s);
return list;
}
编辑:
如果只想打印文字而不想拆分或列单,可以这样用:
public static void main(String[] args) {
newLine("I like cats");
}
public static void newLine(String s){
while(s.contains(" ")){
int pos = s.indexOf(' ');
System.out.println(s.substring(0, pos));
s = s.substring(pos + 1);
}
System.out.println(s);
}
我想这会解决你的问题。
public static List<String> getWords(String text) {
List<String> words = new ArrayList<>();
BreakIterator breakIterator = BreakIterator.getWordInstance();
breakIterator.setText(text);
int lastIndex = breakIterator.first();
while (BreakIterator.DONE != lastIndex) {
int firstIndex = lastIndex;
lastIndex = breakIterator.next();
if (lastIndex != BreakIterator.DONE && Character.isLetterOrDigit(text.charAt(firstIndex))) {
words.add(text.substring(firstIndex, lastIndex));
}
}
return words;
}
public static void main(String[] args) {
String text = "I like cats";
List<String> words = getWords(text);
for (String word : words) {
System.out.println(word);
}
}
输出:
I
like
cats
抱歉,我没有提醒您不能使用 replaceAll()
。
这是我的另一个解决方案:
String s = "I like cats";
Pattern p = Pattern.compile("([\S])+");
Matcher m = p.matcher(s);
while (m.find( )) {
System.out.println(m.group());
}
旧解决方案:
String s = "I like cats";
System.out.println(s.replaceAll("( )+","\n"));
你几乎完成了所有的工作。只需进行少量添加,您的代码就会如您所愿:
for (int i = 0; i < length - 1;) {
j = text.indexOf(" ", i);
if (i == j) { //if next space after space, skip it
i = j + 1;
continue;
}
if (j == -1) {
j = text.length();
}
System.out.print("\n" + text.substring(i, j));
i = j + 1;
}
我正在尝试编写一个程序,允许用户输入一个短语(例如:"I like cats"
)并在单独的行上打印每个单词。我已经编写了允许在每个 space 换行的部分,但我不想在 单词 之间有 空行 ] 因为 过剩 spaces。我不能使用任何 regular expressions
,例如 String.split()
、replaceAll()
或 trim()
。
我尝试使用几种不同的方法,但如果您不知道可能存在的确切数量,我不知道如何删除 space。我尝试了很多不同的 methods
但似乎没有任何效果。
有没有办法将它实现到我已经编写的代码中?
for (i=0; i<length-1;) {
j = text.indexOf(" ", i);
if (j==-1) {
j = text.length();
}
System.out.print("\n"+text.substring(i,j));
i = j+1;
}
或者如何为它写一个新的表达式?任何建议将不胜感激。
I have already written the part to allow a new line at every space but I don't want to have blank lines between the words because of excess spaces.
如果你不会用trim()
或replaceAll()
,你可以用java.util.Scanner
来读每个单词作为令牌。默认情况下 Scanner
使用白色 space 模式作为查找标记的分隔符。同样,您也可以使用 StringTokenizer
在新行上打印每个单词。
String str = "I like cats";
Scanner scanner = new Scanner(str);
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
输出
I
like
cats
像这样的东西呢,它的 O(N) 时间复杂度: 只需在遍历字符串时使用字符串生成器创建字符串,只要找到 space
添加“\n” String word = "I like cats";
StringBuilder sb = new StringBuilder();
boolean newLine = true;
for(int i = 0; i < word.length(); i++) {
if (word.charAt(i) == ' ') {
if (newLine) {
sb.append("\n");
newLine = false;
}
} else {
newLine = true;
sb.append(word.charAt(i));
}
}
String result = sb.toString();
编辑:修复了评论中提到的问题(多个 space 换行)
这是一个使用 substring() 和 indexOf() 的简单解决方案
public static void main(String[] args) {
List<String> split = split("I like cats");
split.forEach(System.out::println);
}
public static List<String> split(String s){
List<String> list = new ArrayList<>();
while(s.contains(" ")){
int pos = s.indexOf(' ');
list.add(s.substring(0, pos));
s = s.substring(pos + 1);
}
list.add(s);
return list;
}
编辑:
如果只想打印文字而不想拆分或列单,可以这样用:
public static void main(String[] args) {
newLine("I like cats");
}
public static void newLine(String s){
while(s.contains(" ")){
int pos = s.indexOf(' ');
System.out.println(s.substring(0, pos));
s = s.substring(pos + 1);
}
System.out.println(s);
}
我想这会解决你的问题。
public static List<String> getWords(String text) {
List<String> words = new ArrayList<>();
BreakIterator breakIterator = BreakIterator.getWordInstance();
breakIterator.setText(text);
int lastIndex = breakIterator.first();
while (BreakIterator.DONE != lastIndex) {
int firstIndex = lastIndex;
lastIndex = breakIterator.next();
if (lastIndex != BreakIterator.DONE && Character.isLetterOrDigit(text.charAt(firstIndex))) {
words.add(text.substring(firstIndex, lastIndex));
}
}
return words;
}
public static void main(String[] args) {
String text = "I like cats";
List<String> words = getWords(text);
for (String word : words) {
System.out.println(word);
}
}
输出:
I
like
cats
抱歉,我没有提醒您不能使用 replaceAll()
。
这是我的另一个解决方案:
String s = "I like cats";
Pattern p = Pattern.compile("([\S])+");
Matcher m = p.matcher(s);
while (m.find( )) {
System.out.println(m.group());
}
旧解决方案:
String s = "I like cats";
System.out.println(s.replaceAll("( )+","\n"));
你几乎完成了所有的工作。只需进行少量添加,您的代码就会如您所愿:
for (int i = 0; i < length - 1;) {
j = text.indexOf(" ", i);
if (i == j) { //if next space after space, skip it
i = j + 1;
continue;
}
if (j == -1) {
j = text.length();
}
System.out.print("\n" + text.substring(i, j));
i = j + 1;
}