如何在java中的字符串中附加反斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6424315/
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-16 05:48:17  来源:igfitidea点击:

How to append a backslash in a string in java

javastring

提问by Batman

I want to add a '\' character to every string in a list of strings... I m doing something like this but it adds 2 backslashes instead.

我想为字符串列表中的每个字符串添加一个 '\' 字符......我正在做这样的事情,但它添加了 2 个反斜杠。

feedbackMsgs.add(behaviorName+"\\"+fbCode);

feedbackMsgs.add(behaviorName+"\\"+fbCode);

result is like: "abc\\def"

结果是这样的: "abc\\def"

how to make sure a single backslash is added??

如何确保添加单个反斜杠?

采纳答案by adarshr

Looks like either your behaviourNameends with a \or fbCodestarts with one.

看起来要么behaviourName以 a 结尾,\要么fbCode以 one 开头。

回答by spot35

I've just run a program with the following -

我刚刚运行了一个具有以下内容的程序 -

String s = "test" + "\" + "test2";
System.out.println(s);

And it prints out the following -

它打印出以下内容 -

test\test2

Are you sure there is no \ in the behaviourNameor fbCodevariables?

您确定behaviourNameorfbCode变量中没有 \吗?

回答by Prashant Bhate

Try to Log/print behaviorNamefbCodeand find it yourself !

尝试记录/打印behaviorNamefbCode并自己找到它!

System.out.println(behaviorName);
System.out.println(fbCode);