Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何删除标点符号,但留下重音字母?_Java_String - Fatal编程技术网

Java 如何删除标点符号,但留下重音字母?

Java 如何删除标点符号,但留下重音字母?,java,string,Java,String,我试图从文本数据中删除标点符号,但保留重音字母。我不想将重音字母替换为英语对应字母。我不知道如何调整现有代码以允许使用更高的ascii字符 while (input.hasNext()){ String phrase = input.nextLine(); String[] words = phrase.split(" "); for(String word: words){ String strippedInp

我试图从文本数据中删除标点符号,但保留重音字母。我不想将重音字母替换为英语对应字母。我不知道如何调整现有代码以允许使用更高的ascii字符

    while (input.hasNext()){
        String phrase = input.nextLine();
        String[] words = phrase.split(" ");
        for(String word: words){
              String strippedInput = word.replaceAll("[^0-9a-zA-Z\\s]", ""); 
        }
     }
如果原始输入为: 哦,萨尔,你是谁,你是谁

预期产出应为: O sal ou O sódio tambémécontracidiado em pacientes hipotensos

有什么想法吗?谢谢

试试这个。
Try this.

public class punctuationRemove {

//private static String punc = "[][(){},.;!?<>%]";
 static StringBuilder sb = new StringBuilder();
 static char[] punc = "',.;!?(){}[]<>%".toCharArray();

 public static void main(String[] args){
        String s = "Hello!, how are you?";
        System.out.println(removePuntuation(s));
    }

 public static String removePuntuation(String s)
 {
     String tmp;
     boolean fl=true;

     for(int i=0;i<s.length();i++)
     {
         fl=true;
         char strChar=s.charAt(i);
         for (char badChar : punc) 
         {
            if (badChar == strChar)
            {
               fl=false;
               break;
             }
          }

          if(fl)
          {
             sb.append(strChar);
           }
     }
     return sb.toString();
 }
}
公共类标点删除{ //私有静态字符串punc=“[[(){},.;!?%]”; 静态StringBuilder sb=新StringBuilder(); 静态字符[]punc=“”,.;!?(){}[]%”。tocharray(); 公共静态void main(字符串[]args){ String s=“你好,你好吗?”; System.out.println(移除Puntuation); } 公共静态字符串removePuntuation(字符串s) { 串tmp; 布尔fl=真;
对于(int i=0;i来说,这可能效率低下,而且我相信这个想法可以改进,但是您可以创建一个方法,在字符串中循环,为不是标点符号的每个字符建立一个缓冲区

private String replacePunctuation(String s){
    String output = "";

    for(int i = 0; i < s.Length(); i++){
        if(s.charAt(i) != '.' && s.charAt(i) != ',' && s.charAt(i) != '!') // Add other punctuation values you're concerned about. Perhaps the Regex class would be useful here, but I am not as familiar with it as I would like.
            output += s.charAt(i);
        }
    }
}
专用字符串替换标点符号(字符串s){
字符串输出=”;
对于(int i=0;i