Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a backslash in a string in java

Tags:

java

string

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.

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

result is like: "abc\\def"

how to make sure a single backslash is added??

like image 216
Batman Avatar asked Jun 21 '11 11:06

Batman


People also ask

How do you add a backslash to a string in Java?

The regular expression \\ matches a single backslash. This regular expression as a Java string, becomes "\\\\".

How do you add a backslash to a string?

If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: var s = "\\Tasks"; // or var s = @"\Tasks"; Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.

How do you split a string with a backslash?

split() method to split a string on the backslashes, e.g. my_list = my_str. split('\\') . The str. split method will split the string on each occurrence of a backslash and will return a list containing the results.


2 Answers

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?

like image 90
spot35 Avatar answered Sep 22 '22 23:09

spot35


Looks like either your behaviourName ends with a \ or fbCode starts with one.

like image 29
adarshr Avatar answered Sep 24 '22 23:09

adarshr