Java从字符串的开头和结尾删除所有非字母数字字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24967089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:02:04  来源:igfitidea点击:

Java remove all non alphanumeric character from beginning and end of string

javaregexstringreplacealphanumeric

提问by Mike6679

I know how to replace ALL non alphanumeric chars in a string but how to do it from just beginning and end of the string?

我知道如何替换字符串中的所有非字母数字字符,但是如何从字符串的开头和结尾进行替换?

I need this string:

我需要这个字符串:

"theString,"

“弦”

to be:

成为:

theString

字符串

replace ALL non alphanumeric chars in a string:

替换字符串中的所有非字母数字字符:

s = s.replaceAll("[^a-zA-Z0-9\s]", "");

采纳答案by falsetru

Use ^(matches at the beginning of the string) and $(matches at the end) anchors:

使用^(在字符串开头匹配)和$(在结尾匹配)锚点:

s = s.replaceAll("^[^a-zA-Z0-9\s]+|[^a-zA-Z0-9\s]+$", "");

回答by ranamiteshkumar

This removes all the non-alphanumeric characters

这将删除所有非字母数字字符

s = s.replaceAll("[^a-zA-Z0-9]", "");

回答by Prasad Bhosale

yourString=yourString.replaceAll("^\W+|\W+$","");

回答by Danielson

Use:

用:

s.replaceAll("^[^\p{L}^\p{N}\s%]+|[^\p{L}^\p{N}\s%]+$", "")

Instead of:

代替:

s.replaceAll("^[^a-zA-Z0-9\s]+|[^a-zA-Z0-9\s]+$", "")

Where p{L}is any kind of letter from any language.
And p{N}is any kind of numeric character in any script.
For use in Latin-based scripts, when non-English languages are needed, like Spanish, for instance: éstas, apuntó; will in the latter become; stas and apunt. The former also works on non-Latin based languages.
For all Indo-European Languages, add p{Mn}for Arabic and Hebrew vowels:

p{L}来自任何语言的任何类型的信件在哪里。
并且p{N}是任何脚本中的任何类型的数字字符。
用于基于拉丁语的脚本,当需要非英语语言时,如西班牙语,例如:éstas、apuntó;将在后者成为;stas 和 apunt。前者也适用于非拉丁语系。
对于所有印欧语言,添加p{Mn}阿拉伯语和希伯来语元音:

s.replaceAll("^[^\p{L}^\p{N}^\p{Mn}\s%]+|[^\p{L}^\p{N}^\p{Mn}\s%]+$", "")

For Dravidian languages, the vowels may surround the consonant - as opposed to Semitic languages where they are "within" the character - like ?. For this use p{Me}instead. For all languages use:

对于达罗毗荼语言,元音可能围绕着辅音——而不是闪米特语,它们在字符“内”——如 ?。用于此用途p{Me}。对于所有语言,请使用:

s.replaceAll("^[^\p{L}^\p{N}^\p{M}\s%]+|[^\p{L}^\p{N}^\p{M}\s%]+$", "")

See regex tutorial for a list of Unicode categories

有关Unicode 类别列表,请参阅正则表达式教程

回答by Bunarro

Guava's CharMatcherprovides a concise solution:

Guava 的CharMatcher提供了一个简洁的解决方案:

CharMatcher.javaLetterOrDigit().negate().trimFrom(input);