Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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_Loops - Fatal编程技术网

Java 未删除特殊字符

Java 未删除特殊字符,java,string,loops,Java,String,Loops,因此,我有一个问题,我的代码将不会删除特殊字符,除了空格。我尝试过创建索引,甚至添加索引,但仍然不起作用。我试过输入“香蕉皮”,但它不起作用 String strMessage = input.nextLine(); String strChars = " !@#$%^&&*() "; for(int i = 0; i < strMessage.length() - 1; i++) for(int

因此,我有一个问题,我的代码将不会删除特殊字符,除了空格。我尝试过创建索引,甚至添加索引,但仍然不起作用。我试过输入“香蕉皮”,但它不起作用

        String strMessage = input.nextLine();

        String strChars = " !@#$%^&&*() ";

        for(int i = 0; i < strMessage.length() - 1; i++)
            for(int j = 0; j < strChars.length(); j++) {
                if(strChars.charAt(j) == strMessage.charAt(i)) {
                    strMessage = strMessage.substring(0, strMessage.indexOf(strMessage.charAt(i))) + strMessage.substring(strMessage.indexOf(strMessage.charAt(i+1)));
                    break;
                }
            }

        System.out.println(strMessage);
String strMessage=input.nextLine();
字符串strChars=“!@$%^&&*()”;
对于(int i=0;i

所以我的输出应该是BANANAPEEL

我认为这样它会很好地工作:

            Scanner input = new Scanner(System.in);
            String strMessage = input.nextLine();
            String strChars = " !@#$%^&&*()";
            String temp = "";
            boolean isInStrChars = false;

            for (int i = 0; i < strMessage.length(); i++){
                isInStrChars = false;
                for (int j = 0; j < strChars.length(); j++) {
                    if (strChars.charAt(j) == strMessage.charAt(i)) {
                        isInStrChars = true;
                    }
                }
                if(!isInStrChars){
                    temp+=strMessage.charAt(i);
                }
            }
            System.out.println(temp);

您可以使用字符串标记器类来分隔字符串

StringTokenizer t = new StringTokenizer(strMessage," !@#$%^&&*()");
while(t.hasMoreElements()){
System.out.print(t.nextToken()+""); }
假设strMessage=“hello$#&w orl#d”

输出为helloworld

使用StringTokenizer----


输出-helloworld

您可以简单地使用String类的

像下面这样

public static void main(String[] args) {
    String source = "hel!lo$#& w orl#d";
    String replacementChar = " |!|@|#|\\$|%|^|&|(|)|\\*"; //Add you char separated by OR( |) 

    String s = source.replaceAll(replacementChar, "");

    System.out.println(s);

}
输出:

地狱世界

编辑:

正如在评论中所讨论的,您不想使用正则表达式,只想保留字母表,也许您可以使用相同的方法来解决

publicstaticvoidmain(字符串[]args)引发IOException{
字符串strMessage=“香蕉皮”;
StringBuilder=新的StringBuilder();
对于(int i=0;i
使用lambda函数删除作为字符串传递的所有字符


可能重复的哦,是的,忘了提到我实际上不允许使用那个东西。你想只保留字母表吗?然后你可以用传统的方式使用字符类来完成,我正在编辑我的答案来帮助你哇,我不知道这是可能的。几天前,这个答案被标记为“正确答案”,现在它甚至不再有用了?发生了什么lol?哦,对不起,我没有意识到你只能有一个正确答案是的,你不能有超过一个被接受的答案,因此,当你接受我的答案时,我投了更高的票:)谢谢!你不必让我的答案成为被接受的答案,我只是觉得让它至少“有用”是公平的。但非常感谢您的好意!:)
 StringTokenizer st1 =
             new StringTokenizer("hello$#& w orl#d", " !@#$%^&&*()");
        while (st1.hasMoreTokens())
            System.out.print(st1.nextToken());
public static void main(String[] args) {
    String source = "hel!lo$#& w orl#d";
    String replacementChar = " |!|@|#|\\$|%|^|&|(|)|\\*"; //Add you char separated by OR( |) 

    String s = source.replaceAll(replacementChar, "");

    System.out.println(s);

}
 public static void main(String[] args) throws IOException {
        String strMessage = "BANANA & PEEL";

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < strMessage.length(); i++) {

            if (Character.isAlphabetic(strMessage.charAt(i))) {
                builder.append(strMessage.charAt(i));
            }
        }
        String finalString = builder.toString();

        System.out.println(finalString);

    }
String strMessage = "BANANA & PEEL";
String strChars = " !@#$%^&&*()";

deleteChars.apply( strMessage, strChars );  // BANANAPEEL