在 Java 字符串中用 \u0023 替换 # [英] Replace # with \u0023 in a Java String

查看:111
本文介绍了在 Java 字符串中用 \u0023 替换 #的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

\u0023 替换 Java 字符串中的 #,如下所示:

Replace # with \u0023 in a Java String which looks like below:

{subjectCategory:"s123", subjectId:"111222333", content:"test #comment999", ownerId:"111", ownerName:"tester"}

{subjectCategory:"s123", subjectId:"111222333", content:"test #comment999", ownerId:"111", ownerName:"tester"}

String.replace("#","\\u0023");

我已经尝试了上述功能,但似乎不起作用.

I've tried the above function, but it doesn't seem to work.

推荐答案

你需要用另一个反斜杠转义反斜杠:

You need to escape the backslash with another backslash:

string = string.replace("#", "\\u0023");

测试:

String s = "hello # world";
s = s.replace("#","\\u0023");
System.out.println(s); // prints hello \u0023 world

这篇关于在 Java 字符串中用 \u0023 替换 #的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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