[英]Java Character removal of a string
How should I go about removing the two characters "[" and "]" from a string 我应该如何从字符串中删除两个字符“ [”和“]”
Example: [45.677]
Desired: 45.677
I would preferably like to preform this in on single handed method call. 我最好在单手方法调用中执行此操作。 Moreover, Not two str.replace() calls.
而且, 没有两个str.replace()调用。
使用正则表达式
string.replaceAll("\\[|\\]","");
use below regex: 在正则表达式下面使用:
System.out.println( "[45.677]".replaceAll("\\[|]",""));
as [
is a special character(meta character) you should escape it with backslash in order to treat it as a normal character. 由于
[
是特殊字符(元字符),因此您应使用反斜杠将其转义以将其视为普通字符。 thus \\\\[
因此
\\\\[
除了使用replaceAll
,如果[]
字符始终位于同一位置(字符串的开头和结尾),则还可以使用:
string.substring(1, string.length() - 1);
尝试这个
System.out.println("[45.677]".replaceAll("[\\[\\]]", ""));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.