-2

Let's say, I write some code to print out "Hello World."

Could I use some sort of ignore list of characters to be ignored from my print string (in this case Hello World). For instance, I want to remove the r in "hello world" so that the output is "Hello Wold"

I have a value that I am using. I have the following characters: &, \, and *.

I think one of these characters is causing me to have some of my values ignored.

Normally I would just test this myself, but I am not able to do this at this time.

Suparna
  • 1,132
  • 1
  • 8
  • 20

2 Answers2

2

No idea why you would want to do this, but writing a method to do that isn't all that hard...

public static void printlnWithIgnores(String toPrint, Set<Character> ignore) {
    for(char c : toPrint.toCharArray()) {
        if(! ignore.contains(c)) {
            System.out.print(c);
        }
    }
    System.out.println();
}

As for that happening within a string because of a character literal, I'm not sure that it's possible. Some tests:

  • \b (the backspace character) doesn't work
  • (char)127 (the delete character) doesn't work

That said, if your passwords can have backslashes (\) in them, that can absolutely be a problem. \ in Java is used to denote a special character, with the following character. The string "nnn" is just "nnn", but "n\nn" is

 n
 n

Because \n represents a newline, the third n is lost.

There are many specialty characters denoted like this, but more importantly your passwords really can't have \ in them without causing issues. Either you're getting an escape character if the following character if the following character with a backslash is legal (and \1 actually is), or it won't print if it's not legal.

Community
  • 1
  • 1
Mshnik
  • 7,032
  • 1
  • 25
  • 38
  • I am using a program written in Java. I have created a password. My password contains those characters. I think those characters are causing my password to have issues because it is having an effect on the characters following it. I could just change my password, but I wanted to see if this was the problem first. – Chicken Sandwich No Pickles Apr 01 '16 at 00:48
  • What are sort of things you want to ignore? Do you have a list of such characters? – Suparna Apr 01 '16 at 00:54
  • I don't want to ignore anything. I have a value set to "\*" (without the quotes) and I think that is causing me problems. Is using a backward slash a bad idea? Let's say I create a user login on a program using Java. Wouldn't this cause issues? Like if I made my username "L\unchBox." – Chicken Sandwich No Pickles Apr 01 '16 at 00:58
  • @Mshnik, what if I did something that typically didn't have any meaning. Like you said, "\n" starts a new line, but what about something like "\9" or "\1" etc? Would this still cause problems? – Chicken Sandwich No Pickles Apr 01 '16 at 01:00
  • Unfortunately, yes. Edited. – Mshnik Apr 01 '16 at 01:01
0

Solving this same problem using Java 8:

    List<String> ignore = Arrays.asList("r", "2", "3");
    String text = "Hello world 1 2 3";
    text=toRemove.stream().map(toRem-> (Function<String,String>)s->s.replaceAll(toRem, "")).reduce(Function.identity(), Function::andThen).apply(text);
    System.out.println(text);

Output:

Hello wold 1  
  
a121
  • 798
  • 4
  • 9
  • 20
Suparna
  • 1,132
  • 1
  • 8
  • 20