Kotha
Kotha

Reputation: 33

Using Java 8 Streams replace Characters in a String if found in a list of Characters

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¦, _, $

Upvotes: 1

Views: 15928

Answers (2)

Holger
Holger

Reputation: 298389

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.

Upvotes: 11

Eugene
Eugene

Reputation: 120968

Seems trivial enough:

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

OR

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

Upvotes: 1

Related Questions