在java中,如何检查字符串的conrequisive特殊字符是否超过2个?

在java中,如何检查字符串的conrequisive特殊字符是否超过2个?,java,Java,我正在尝试检查字符串是否有超过2次的特殊字符。 我从列表中获得的字符串使用以下代码: List <String> lst; for(String str: lst) { System.out.println(str); } 我希望输出如下内容: This is sample example. This will help you for sure "my friend" do not ask to delay more. $$200 not much better to go ho

我正在尝试检查字符串是否有超过2次的特殊字符。
我从列表中获得的字符串使用以下代码:

List <String> lst;
for(String str: lst)
{
System.out.println(str);
}
我希望输出如下内容:

This is sample example.
This will help you for sure
"my friend" do not ask to delay more.
$$200 not much
better to go home.

如何使用JAVA实现此输出?请建议一种方法。

制作一个字符数组并搜索每个字符,查看是否出现3个特殊字符,如果出现,请删除(或者在我下面的例子中,不要将该字符串复制到将用于打印的新列表中。我没有删除SSS字符串,因为它不是特殊字符,但是如果您添加了一个if并检查了3个连续字符是否具有相同的值,您也可以这样做

public static void main(String[] args) {
       List <String> lst = new ArrayList<>();
       List <String> newList = new ArrayList<>();
       lst.add("This is sample example.");
       lst.add("########################");
       lst.add("This will help you for sure");
       lst.add("\"my friend\" do not ask to delay more.");
       lst.add("$$200 not much");
       lst.add("sssssss");
       lst.add("better to go home.");

       for(int i = 0; i < lst.size(); i++) {
           boolean keep = true;
           char[] c = lst.get(i).toCharArray();
           for(int j = 0; j < c.length; j++) { 

//the following line can be edited based on what you consider special characters
publicstaticvoidmain(字符串[]args){
List lst=new ArrayList();
List newList=newarraylist();
添加(“这是示例”);
1.加上(“#########################”;
添加(“这肯定会帮助你”);
加上(“\”我的朋友“\”不要要求再拖延了。”);
一、加上(“$$200不多”);
第1条添加(“SSS”);
加上(“最好回家。”);
对于(inti=0;i
//但这将允许所有数字和字母

               if(j+2 < c.length && (c[j] < 48 || c[j] > 122 ||(c[j] > 57 && c[j] < 65))) {
                   if(c[j+1] < 65 || c[j+1] > 122 ||(c[j] > 57 && c[j] < 65)) {
                       if(c[j+2] < 65 || c[j+2] > 122 ||(c[j] > 57 && c[j] < 65)) {
                           keep = false;
                       }
                   }
               }
           }
           if(keep) {
               newList.add(lst.get(i));
           }
       }

       for(String str: newList)
    {
    System.out.println(str);
    }
    }
if(j+2122 | |(c[j]>57&&c[j]<65))){
如果(c[j+1]<65 | c[j+1]>122 | |(c[j]>57&&c[j]<65)){
如果(c[j+2]<65 | | c[j+2]>122 | |(c[j]>57&&c[j]<65)){
保持=假;
}
}
}
}
如果(保留){
newList.add(lst.get(i));
}
}
for(字符串str:newList)
{
系统输出打印项次(str);
}
}
试试这个正则表达式:
string.replaceAll(“^.*.[.\u\'!\$%&*+\\\/=?{124;}~\\\\-]{2}.*.*$”,“)

那么您给出的示例是一个字符串(带“\n”)或者它们是不同的输入?@anandudavia这些是不同的字符串。逐行。@anandudavia你可以看到我编辑过的问题一次。为什么
sssss
是特殊字符?试试这个
string.replaceAll(“^.*.[.\u\'!\\\\$%&*+\\\/=?{124; ~
\\\\\-]{2}.*$”,“”)n、 因此,你可能会投反对票。对这个表达的解释很可能也会对OP有所帮助。你的评论regex对我很有帮助…:)谢谢。
               if(j+2 < c.length && (c[j] < 48 || c[j] > 122 ||(c[j] > 57 && c[j] < 65))) {
                   if(c[j+1] < 65 || c[j+1] > 122 ||(c[j] > 57 && c[j] < 65)) {
                       if(c[j+2] < 65 || c[j+2] > 122 ||(c[j] > 57 && c[j] < 65)) {
                           keep = false;
                       }
                   }
               }
           }
           if(keep) {
               newList.add(lst.get(i));
           }
       }

       for(String str: newList)
    {
    System.out.println(str);
    }
    }