如何在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
How to append a backslash in a string in java
提问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 behaviourName
ends with a \
or fbCode
starts 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 behaviourName
or fbCode
variables?
您确定behaviourName
orfbCode
变量中没有 \吗?
回答by Prashant Bhate
Try to Log/print behaviorName
fbCode
and find it yourself !
尝试记录/打印behaviorName
fbCode
并自己找到它!
System.out.println(behaviorName);
System.out.println(fbCode);