Java 如何从字符串中删除符号并将其放回?
全局任务是将文本中最长的单词替换为最短的单词。 我需要删除符号,例如Java 如何从字符串中删除符号并将其放回?,java,Java,全局任务是将文本中最长的单词替换为最短的单词。 我需要删除符号,例如从文本(计算单词长度)和更新单词数组,做一些逻辑,然后放回 我的解决方案: Map idxToSymbolMap=newhashmap(); //删除符号并将其保存到地图 无效删除符号(字符串[]数组){ 字符串[]符号={点、问号、感叹号、逗号}; for(int i=0;i
代码>从文本(计算单词长度)和更新单词数组,做一些逻辑,然后放回
我的解决方案:
Map idxToSymbolMap=newhashmap();
//删除符号并将其保存到地图
无效删除符号(字符串[]数组){
字符串[]符号={点、问号、感叹号、逗号};
for(int i=0;i{
if(k==idx){
数组[idx]+=v;
}
});
}
}
我怎么才能做得更好,清洁工?可以使用replaceAll或其他方法吗?我想你的方法是正确的,我想
但是,您可以使其更干净:
String[] symbols = {".", "?", "!", ","};
for (int i = 0; i < array.length; i++) {
for (String s: symbols) {
if (array[i].contains(s)) {
array[i] = array[i].replace(s, "");
idxToSymbolMap.put(i, s);
}
}
}
并将其替换为:
IntStream.range(0, array.length).forEach(
idx -> idxToSymbolMap.keySet().stream().filter(key -> key == idx)
.forEach(key -> array[idx] += idxToSymbolMap.get(key)));
Map
最适合此要求,其中键是符号,值是文本中符号所在的索引列表
为了进行测试,让我们将每次出现的[,!?]
替换为#
,打印替换的字符串并返回原始字符串
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Map<Character, List<Integer>> symbolToIdxMap = new HashMap<>();
// Test string
String text = "abc .123, xyz! hello . hi";
// Regex pattern
String strPattern = "[\\.\\,\\!\\?]";
Pattern pattern = Pattern.compile(strPattern);
Matcher matcher = pattern.matcher(text);
int index;
// Store the symbols and their indices in the map
while (matcher.find()) {
// Index at which any of [.,!?] is found
index = matcher.start();
// Character at `index`
Character symbol = text.charAt(index);
// If the List<Integer> for `symbol` exists, get it return a new
// ArrayList<Integer> to `idxList`
List<Integer> idxList = symbolToIdxMap.getOrDefault(symbol, new ArrayList<Integer>());
// Add `index` to `idxList`
idxList.add(index);
// Put `idxList` as the value of `symbol`
symbolToIdxMap.put(symbol, idxList);
}
// Replace the symbols in the text
String replaced = text.replaceAll(strPattern, "#");
System.out.println("Replaced: " + replaced);
// Restore original string
StringBuilder sb = new StringBuilder(replaced);
for (Entry<Character, List<Integer>> entry : symbolToIdxMap.entrySet()) {
// Replace the character at all indices in the list with the corresponding
// character
for (Integer idx : entry.getValue()) {
sb.setCharAt(idx, entry.getKey());
}
}
// Display original string
System.out.println("Original: " + sb);
}
}
words[idx]=words[idx]+值代码>-这将在末尾附加符号。不是它被替换的位置。这就是你想要的吗?我想,没关系,因为这些符号总是停留在单词的末尾,对吗?例如,请看我的评论:)听起来像是一个。你到底想做什么?你能在你的问题中加入样本输入和输出吗?我不明白你想在这里做什么。输入是伦敦是英国的首都,但荷兰的阿姆斯特丹。
输出是伦敦荷兰是英国的首都,但荷兰的阿姆斯特丹是。
整个任务是用最长的单词替换第一个匹配的最短单词谢谢,但这个例子不起作用。第一个示例交换了字母,包括上一个位置的symbol和left symbol(复制的符号),第二个返回的字符串带有双逗号,第三个从起始位置删除符号并添加到newMy bad。我没有添加代码行来替换它!现在一切正常。OP的评论说他们需要计算字符数:问题中每个符号的数量已经在地图上了。您只需要获取值的大小,例如System.out.println(symbolToIdxMap.get(Character.valueOf('.')).size())
@ArvindKumarAvinash我的意思是我需要计算数组中每个字符串元素(不带符号的单词)的长度:)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Map<Character, List<Integer>> symbolToIdxMap = new HashMap<>();
// Test string
String text = "abc .123, xyz! hello . hi";
// Regex pattern
String strPattern = "[\\.\\,\\!\\?]";
Pattern pattern = Pattern.compile(strPattern);
Matcher matcher = pattern.matcher(text);
int index;
// Store the symbols and their indices in the map
while (matcher.find()) {
// Index at which any of [.,!?] is found
index = matcher.start();
// Character at `index`
Character symbol = text.charAt(index);
// If the List<Integer> for `symbol` exists, get it return a new
// ArrayList<Integer> to `idxList`
List<Integer> idxList = symbolToIdxMap.getOrDefault(symbol, new ArrayList<Integer>());
// Add `index` to `idxList`
idxList.add(index);
// Put `idxList` as the value of `symbol`
symbolToIdxMap.put(symbol, idxList);
}
// Replace the symbols in the text
String replaced = text.replaceAll(strPattern, "#");
System.out.println("Replaced: " + replaced);
// Restore original string
StringBuilder sb = new StringBuilder(replaced);
for (Entry<Character, List<Integer>> entry : symbolToIdxMap.entrySet()) {
// Replace the character at all indices in the list with the corresponding
// character
for (Integer idx : entry.getValue()) {
sb.setCharAt(idx, entry.getKey());
}
}
// Display original string
System.out.println("Original: " + sb);
}
}
Replaced: abc #123# xyz# hello # hi
Original: abc .123, xyz! hello . hi