英文:
Deleting whitespace within a string array
问题
以下是已经翻译好的内容:
这段代码的目的是检查一个字符串数组,看看单词之间是否有空格。这里有一个示例:{"every", "near ing", "checking", "food ", "stand", "value "。应该修改为:{"every", "nearing", "checking", "food", "stand", "value"}。以下是我目前的代码:
package space;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class spacefinder {
public static void main(String[] args) {
String[] arr = {"every", "near ing", "checking", "food", "stand", "value"};
Pattern pattern = Pattern.compile("\\s");
for (int i = 0; i < arr.length; i++) {
Matcher matcher = pattern.matcher(arr[i]);
arr[i] = matcher.replaceAll(""); // 移除空格
}
}
}
这段代码会导致错误,而且我看到的每个教程都没有使用字符串数组,比如我这种情况下的 String[] Arr,它们只使用普通的 String = 语句。
英文:
This code is supposed to check through an array of strings and see if there is a space between a word. Here is an example: {“every”, “near ing”, “ checking”, “food “, “stand”, “value “}. It should be changed to hold: {“every”, “nearing”, “checking”, “food”, “stand”, “value”}. Here is my code so far:
package space;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class spacefinder {
public static void main (String[] args) {
String[] arr = {"every", "near ing", "checking", "food", "stand", "value"};
Pattern pattern = Pattern.compile("\\arr");
Matcher matcher = pattern.matcher(arr);
boolean found = matcher.find();
}
}
This code results in an error, and every tutorial I see does not use a string array, such as the String[] Arr in my case, they only use a regular String = statement.
答案1
得分: 2
在您不需要使用正则表达式和匹配的情况下,我尝试将原始数组包装在列表中,并在列表上使用流来对每个字符串应用replaceAll()。
String[] arr = {"every", "near ing", "checking", "food", "stand", "value"};
List<String> strings = Arrays.asList(arr);
strings.replaceAll(s -> s.replaceAll("\\s", ""));
for (int i = 0; i < strings.size(); i++)
System.out.println(strings.get(i));
输出:
every
nearing
checking
food
stand
value
英文:
In case you don't need to use regex and match, I tried to wrap the primitive array with List and use stream on it to apply replaceAll() on each string on the list.
String[] arr = {"every", "near ing", "checking", "food", "stand", "value"};
List<String> strings = Arrays.asList(arr);
strings.replaceAll(s -> s.replaceAll("\\s",""));
for(int i=0;i< strings.size();i++)
System.out.println(strings.get(i));
output :
every
nearing
checking
food
stand
value
答案2
得分: 0
如果您必须使用正则表达式并进行匹配:(单词)(空格)(单词),那么您可以:
String[] arr = {"every", "near ing", "checking", "food", "stand", "value"};
Pattern pattern = Pattern.compile("\\w\\s\\w");
boolean found = false;
for (String item : arr) {
Matcher matcher = pattern.matcher(item);
found = matcher.find();
if (found) {
break;
}
}
您代码中的问题是您正在尝试使用 pattern.matcher(arr)
这样的方法来匹配一个模式,这是错误的。matcher
方法需要一个字符串值作为参数。
上述代码首先遍历数组,匹配所有由两个单词用空格分隔的字符串。
因为在问题中没有指定在字符串匹配模式时应该发生什么,所以上述代码在第一次匹配后终止。
英文:
If you have to use regex and match: (word)(whitespace)(word) then you can:
String[] arr = {"every", "near ing", "checking", "food", "stand", "value"};
Pattern pattern = Pattern.compile("\\w\\s\\w");
boolean found = false;
for (String item : arr) {
Matcher matcher = pattern.matcher(item);
found = matcher.find();
if (found) {
break;
}
}
The problem in your code is that you are trying to match a pattern with an area pattern.matcher(arr)
which is wrong. The matcher method takes a String value.
The above code iterates through the array first and matches all strings that consist of two words separated by a whitespace.
Because it was not specified in the question what should happen when when a sting matches the pattern, the above code terminates after the first match.
答案3
得分: 0
首先,您需要修正正则表达式以查找空格,例如 Pattern.compile("\\s+")
。
其次,您需要遍历数组,并对每个字符串进行替换:
String[] arr = {"every", "near ing", "checking", "food", "stand", "value"};
Pattern pattern = Pattern.compile("\\s+");
for (int i = 0; i < arr.length; i++) {
arr[i] = pattern.matcher(arr[i]).replaceAll("");
}
System.out.println(Arrays.toString(arr));
输出
[every, nearing, checking, food, stand, value]
英文:
First, you need to fix the regex to look for spaces, e.g. Pattern.compile("\\s+")
.
Second, you need to iterate the array and do the replacement for each string:
String[] arr = {"every", "near ing", "checking", "food", "stand", "value"};
Pattern pattern = Pattern.compile("\\s+");
for (int i = 0; i < arr.length; i++) {
arr[i] = pattern.matcher(arr[i]).replaceAll("");
}
System.out.println(Arrays.toString(arr));
Output
[every, nearing, checking, food, stand, value]
答案4
得分: 0
String#replace
您无需使用任何正则表达式;您可以通过使用String#replace
来实现,它将此字符串的每个与字面目标序列匹配的子字符串替换为指定的字面替换序列。
演示:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] arr = { "every", "near ing", "checking", "food", "stand", "value" };
System.out.println("Before: " + Arrays.toString(arr));
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i].replace(" ", "");
}
System.out.println("After: " + Arrays.toString(arr));
}
}
输出:
Before: [every, near ing, checking, food, stand, value]
After: [every, nearing, checking, food, stand, value]
英文:
String#replace
You do not need to use any regex; you can do it simply by using String#replace
which replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
Demo:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] arr = { "every", "near ing", "checking", "food", "stand", "value" };
System.out.println("Before: " + Arrays.toString(arr));
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i].replace(" ", "");
}
System.out.println("After: " + Arrays.toString(arr));
}
}
Output:
Before: [every, near ing, checking, food, stand, value]
After: [every, nearing, checking, food, stand, value]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论