如何删除字符串中的任何插入符号?

发布于 2024-12-16 12:33:07 字数 291 浏览 3 评论 0原文

我使用的正则表达式字符串在其内部某处包含克拉 (^) 符号。 Java中有没有办法删除这些符号?以下是我尝试过的几种方法:

string = "some^string";

string = string.replaceAll("\\^", "");
string = string.replaceAll(Pattern.quote("\\^"), "");
string = string.replaceAll(Pattern.quote("\u2038"), "");

没有一个有效。我缺少什么?

I'm using a regex string that contains a carat (^) symbol somewhere inside of it. Is there a way in Java to remove these symbols? Here are a few methods I've tried:

string = "some^string";

string = string.replaceAll("\\^", "");
string = string.replaceAll(Pattern.quote("\\^"), "");
string = string.replaceAll(Pattern.quote("\u2038"), "");

None of which have worked. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千纸鹤带着心事 2024-12-23 12:33:07

根本不需要使用正则表达式:

string = string.replace("^", "");

但是,您的第一个示例也有效:

public class Test {   
    public static void main(String[] args) throws Exception {
        String string = "some^string";
        string = string.replaceAll("\\^", "");
        System.out.println(string); // Prints somestring
    }
}

...所以您的问题完全有可能在其他地方。

There's no need to use regular expressions at all:

string = string.replace("^", "");

However, the first of your examples works too:

public class Test {   
    public static void main(String[] args) throws Exception {
        String string = "some^string";
        string = string.replaceAll("\\^", "");
        System.out.println(string); // Prints somestring
    }
}

... so it's entirely possible that your problem is elsewhere.

傲影 2024-12-23 12:33:07

string.replaceAll("\\^", ""); 应该可以。

string.replaceAll("\\^", ""); should work.

琉璃梦幻 2024-12-23 12:33:07

请。删除代码上方的最后两行,然后在下面写一些内容,然后再检查。

字符串=“一些^字符串”;

string = string.replaceAll("\^", "");

Pls. delete last 2 lines from above your code and do write below things only then check.

string = "some^string";

string = string.replaceAll("\^", "");

自找没趣 2024-12-23 12:33:07

我认为您可以尝试使用另一个变量名称,例如:

String str1 = string.replace("^", "1");

并使用新的 str1,而不是旧的 string

I think you can try using another variable name, like for example:

String str1 = string.replace("^", "1");

and use the new str1, instead of the old string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文