如何删除Java中除空格或数字之外的所有标点符号。
"\\p{Punct}|\\d", "" //THIS WORKS BUT IT REMOVES THE NUMBERS AND I DONT WANT IT TO REMOVE THE NUMBERS...
我正在阅读文字,我需要删除标点符号。
String[] internal;
char ch = 'a';
int counter = 1;
int count;
int c;
Map<String, Set> dictionary = new HashMap<String, Set>();
BufferedReader in = new BufferedReader(new FileReader("yu.txt"));
while (in.ready()) {
internal = (((in.readLine()).replaceAll("\\p{Punct}|\\d", "")).toLowerCase()).split(" ");//this does not work in my case cause it removes numbers... and makes them whitespaces but other than that this one works I JUST dont want it to remove numbers and keep whitespaces...
for (count = 0; count < internal.length; count++) {
if (!dictionary.containsKey(internal[count])) {
dictionary.put(internal[count], new HashSet());
}
if (dictionary.get(internal[count]).size()<10)
{
dictionary.get(internal[count]).add(counter);
}
}
counter++;
}
Iterator iterator = dictionary.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
String value = dictionary.get(key).toString();
System.out.println(key + ": " + value );
}
答案 0 :(得分:0)
str = str.replaceAll(“[^ 0-9a-zA-Z \ s]”,“X”);
答案 1 :(得分:0)
我不知道现有的类(默认)可以这样做。
您需要编写一个逐字符串字符串的逻辑,并检查该字符是否为标点符号。如果是,则在之前剪切String一个char并附加其余部分(有效地删除该char /标点符号)。
首选使用StringBuilder或StringBuffer而不是直接操作String。
使用String.substring()方法剪切字符串。
否则使用String.replace()/ String.replaceAll()方法用&#34;&#34;替换所有标点符号(您需要转义某些字符)。