如何从字符串中删除无效字符? [英] How to remove invalid characters from a string?

查看:98
本文介绍了如何从字符串中删除无效字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何从Java中的字符串中删除无效字符。我正在尝试删除所有不是数字,字母或()[]的字符。我该怎么做?

I have no idea how to remove invalid characters from a string in Java. I'm trying to remove all the characters that are not numbers, letters, or ( ) [ ] . How can I do this?

谢谢

推荐答案

String foo = "this is a thing with & in it";
foo = foo.replaceAll("[^A-Za-z0-9()\\[\\]]", "");

Javadocs是你的朋友。正则表达式也是你的朋友。

Javadocs are your friend. Regular expressions are also your friend.

编辑:

这就是siad,这只适用于拉丁字母;你可以相应调整。 \\\\ 可用于 a-zA-Z 表示单词字符,如果有效对于你的情况,虽然它包括 _

That being siad, this is only for the Latin alphabet; you can adjust accordingly. \\w can be used for a-zA-Z to denote a "word" character if that works for your case though it includes _.

这篇关于如何从字符串中删除无效字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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