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.
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.
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.
Seems trivial enough:
System.out.println(input.replaceAll("[^a-zA-Z]", " "));
OR
System.out.println(input.replaceAll("[^\\p{L}]", " "));
input.replaceAll(forbidden.stream().map(String::valueOf).map(Pattern::quote) .collect(Collectors.joining("|")), " ")
java-streamtag here|String.replaceAll()easily._appears twice andP¦is not a single character.