Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace list of characters in a string

Tags:

java

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)
like image 791
user373201 Avatar asked Nov 16 '11 15:11

user373201


People also ask

How do I replace multiple characters in a string?

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.

How do you replace letters in a string list?

If you want to replace the string of elements of a list, use the string method replace() for each element with the list comprehension.

Can I replace multiple characters in a string Python?

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.


2 Answers

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("[\\(]", "")

like image 110
AlexR Avatar answered Oct 06 '22 00:10

AlexR


Take a look at the StringUtils class in Apache commons lang API specifically the replaceEach function

like image 34
maneesh Avatar answered Oct 05 '22 23:10

maneesh