1

Input = M+a?i*n S=t

expectedvalue = M a i n s t

Compare each character from Input with the following list of characters and if found replace it with space and the ouput should be expectedValue.

., (, :, ;, ), {, *, +, _, -, !, ?,~, %, =, ¦, ¬, ¢, \, <, >, &, ,, }, $, '', `, @, [, ], ±, Ñ, P¦, _, $

5
  • Looking at your character list, it feels as though you are going about this backwards. Change every character that isn't in a whitelist (A-Z, a-z, ...) to a space. Commented Nov 9, 2017 at 14:46
  • I can't tell why is there is java-stream tag here Commented Nov 9, 2017 at 14:58
  • Maybe because it's homework and it has to be done with streams. Also, whitelist approach won't cut it I think. The problem specifically asks for a blacklist, and since the input charset is unknown, it would have to be a looong whitelist. But @Eugene 's regex approach could be changed, just concat all the forbidden chars with | Commented Nov 9, 2017 at 15:00
  • 1
    Kotha, is using streams really a requirement? It could be done by String.replaceAll() easily. Commented Nov 9, 2017 at 15:06
  • 3
    It’s an odd list. _ appears twice and is not a single character. Commented Nov 9, 2017 at 19:05

2 Answers 2

11

Assuming

String input;
List<Character> forbidden;

your can use

String result = input.replaceAll(
     forbidden.stream()
              .map(String::valueOf).map‌​(Pattern::quote)
              .collect(Collectors.joining("|")),
     " ");

It converts each forbidden Character to a String using String.valueOf(…), then uses Pattern.quote(…) to convert them to a quoted pattern string, which ensures that the characters are not interpreted as special regex constructs, then all are joined using | which means “or” in a regex pattern. Then, String.replaceAll(regex, replacement) does the actual job.

An alternative without regex (“pure stream operation”) would be

String result = input.chars()
    .map(c -> forbidden.contains((char)c)? ' ': c)
    .collect(StringBuilder::new, (sb, c) -> sb.append((char)c), StringBuilder::append)
    .toString();

This straight-forwardly iterates over all characters, checks whether they are contained in the forbidden collection and replaces them with a space if they are, and finally collects the mapped characters to a new String. It would be more efficient when forbidden gets changed to a Set<Character> rather than List<Character> due to the time complexity of contains, which is invoked for every character.

Sign up to request clarification or add additional context in comments.

Comments

1

Seems trivial enough:

System.out.println(input.replaceAll("[^a-zA-Z]", " "));  

OR

System.out.println(input.replaceAll("[^\\p{L}]", " "));

5 Comments

You could use the list of forbidden chars by concating them with "|". Then you would make sure to only replace forbidden chars. Furthermore, this solution does not lowercase chars as shown in the example.
the list I need to search is a custom list.so can't use RegEx to replace all non Alpha Caracters
Of course you can, use ".|(|:|andSoOn" as your regex
input.replaceAll(forbidden.stream().map(String::valueOf).map(Pattern::quote) .collect(Collectors.joining("|")), " ")
@Holger That is a perfect solution for me. It worked like a charm. Thanks a lot. Can you please make this is an answer, so that I can accept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.