如何删除java中除空格或数字以外的所有标点符号
如何删除Java中除空格或数字以外的所有标点符号如何删除java中除空格或数字以外的所有标点符号,java,Java,如何删除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> dictionar
"\\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 );
}
String[]内部;
char ch='a';
int计数器=1;
整数计数;
INTC;
Map dictionary=newhashmap();
BufferedReader in=新的BufferedReader(新文件阅读器(“yu.txt”);
while(在.ready()中){
internal=(((in.readLine()).replaceAll(\\p{Punct}|\\d“,”).toLowerCase()).split(“”;//在我的情况下,这不起作用,因为它会删除数字…并使它们成为空白,但除此之外,我不希望它删除数字并保留空白。。。
用于(计数=0;计数<内部.length;计数++){
if(!dictionary.containsKey(内部[count])){
put(内部[count],新HashSet());
}
如果(dictionary.get(internal[count]).size()str=str.replaceAll([^0-9a-zA-Z\s],“X”);我不知道现有的类(默认)可以这样做
您需要编写一个逻辑,逐个字符地遍历字符串,并检查该字符是否为标点符号。如果为标点符号,则在该字符串前面剪切一个字符,并附加其余部分(有效地删除该字符/标点符号)
更喜欢使用StringBuilder或StringBuffer,而不是直接操作字符串
使用String.substring()方法剪切字符串
否则,请使用String.replace()/String.replaceAll()方法将所有标点符号(您需要将某些字符转义)替换为“.”到目前为止,您有什么资料?至少张贴出来,用户可以给出提示,而不仅仅是在这里直接为您做作业?