如何使用Java的String.replaceAll方法替换加号字符 [英] How to replace a plus character using Java's String.replaceAll method

查看:1679
本文介绍了如何使用Java的String.replaceAll方法替换加号字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于加号字符(+)的正确正则表达式是String类中Java的 replaceAll 方法的第一个参数(即要替换的字符串)是什么?我无法正确理解语法。

What's the correct regex for a plus character (+) as the first argument (i.e. the string to replace) to Java's replaceAll method in the String class? I can't get the syntax right.

推荐答案

你需要逃避 + 表示正则表达式,使用 \

You need to escape the + for the regular expression, using \.

但是,Java使用String参数构造常规表达式,它使用 \ 作为自己的转义序列。所以你必须逃避 \ 本身:

However, Java uses a String parameter to construct regular expressions, which uses \ for its own escape sequences. So you have to escape the \ itself:

"\\+"

这篇关于如何使用Java的String.replaceAll方法替换加号字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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