替换字符串中的字符列表 [英] replace list of characters in a string

查看:420
本文介绍了替换字符串中的字符列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,它是网页的标题。所以它可以< >和其他特殊的字符。

I have a String which is the title of a webpage. so it can have < > and other special charecters in them.

我想写一个函数,它将取一个字符串并替换一个charecters列表。试图找到最好的方法来做到这一点。

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我使用列表或数组或枚举来保存特殊字符列表,或者java中有什么东西可以做到这一点。

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)


推荐答案

str.replaceAll([<>],)

全部放入引号之间的特殊字符。此语句使用正则表达式,因此请小心转义特殊于正则表达式的字符。例如,如果你想要替换你应该说 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("[\\(]", "")

这篇关于替换字符串中的字符列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆