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)
Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.
If you want to replace the string of elements of a list, use the string method replace() for each element with the list comprehension.
01) Using replace() method Python offers replace() method to deal with replacing characters (single or multiple) in a string. The replace method returns a new object (string) replacing specified fields (characters) with new values.
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("[\\(]", "")
Take a look at the StringUtils class in Apache commons lang API specifically the replaceEach function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With