0

So Im having an issue where my code will not remove special characters except spaces. I have tried subtracing indexes and even adding index and it still will not work. I have tried an input "BANANA & PEEL" and it wont work.

        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);

So my output should be BANANAPEEL

user207421
  • 305,947
  • 44
  • 307
  • 483
Salim
  • 33
  • 5
  • 1
    Possible duplicate of [Remove special characters in the string in java?](https://stackoverflow.com/questions/21074485/remove-special-characters-in-the-string-in-java) – Yuvals Sep 27 '19 at 00:50

5 Answers5

1

I think this way it will work quite well:

            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);

I just created a 'temp'-String, and only if a character from the original String is NOT inside the String strChars, it will be added to the 'temp'-String.

EDIT: of course you can set the temp-String as the original strMessage at the end:

strMessage = temp;
GitPhilter
  • 171
  • 9
  • Wow, I didn´t know this was possible. This answer was marked "the right answer" a few days ago and now it is not even helpful anymore? What happened lol? – GitPhilter Sep 30 '19 at 23:47
  • oh sorry i didnt realize you can only have 1 right answer – Salim Oct 03 '19 at 10:52
  • Yes you can't have more than one accepted answer , hence I upvoted it as you accepted my answer :) – Shailesh Chandra Oct 03 '19 at 11:03
  • Thank you! You don´t have to make my answer the accepted one, I just thought it would be fair to make it at least "helpful". but thank you very much for your kindness! :) – GitPhilter Oct 03 '19 at 13:00
0

You can use String Tokenizer class for separating your string.

StringTokenizer t = new StringTokenizer(strMessage," !@#$%^&&*()");
while(t.hasMoreElements()){
System.out.print(t.nextToken()+""); }

suppose strMessage = "hello$#& w orl#d";

the output is helloworld

Habib Mhamadi
  • 729
  • 1
  • 6
  • 15
0

Use StringTokenizer----

 StringTokenizer st1 =
             new StringTokenizer("hello$#& w orl#d", " !@#$%^&&*()");
        while (st1.hasMoreTokens())
            System.out.print(st1.nextToken());

Output - helloworld

0

you could simply use String class' replaceAll(String regex, String replacement)

Something like below

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);

}

output:

helloworld

Edit:

As discussed in comments you don't want to use RegEx and only want to retain alphabets probably you can solve same using Character.isAlphabetic

 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);

    }
Shailesh Chandra
  • 2,164
  • 2
  • 17
  • 25
0

use a lambda function which removes all characters passed as string

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

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

find deleteChars here

Kaplan
  • 2,572
  • 13
  • 14