Reputation: 11445
I have a String which is the title of a webpage. so it can have < > and other special charecters in them.
I want to write a function that will take a string and replace a list of charecters. Trying to find the best way to do it.
Shoud I use a list or array or enums to hold the list of special charecters or is there something in java that will already do this.
filterText(String text, List specialCharecters)
filterText(String text, Array specialCharecters)
filterText(String text, Enum specialCharecters)
Upvotes: 5
Views: 20222
Reputation: 1112
Take a look at the StringUtils class in Apache commons lang API specifically the replaceEach function
Upvotes: 6
Reputation: 115368
str.replaceAll("[<>]", "")
Put all your special characters between the quotes. This statement is using regular expression, so care about escaping characters that are special for regular expression. For example if you want to replace (
you should say str.replaceAll("[\\(]", "")
Upvotes: 6