Java中的反斜杠 [英] Backslash in java
问题描述
我对保存在数据库中的字符串有问题,例如:"311 \ 315_316 \ 336_337"
.它们只有一个反斜杠,这在Java中是一个问题.当我尝试 s.replaceAll("\","\\")
时不起作用,因为Java编译器读取字符串"311 \ 315_316 \ 336_337"
作为311Í_316Þ_337"
.
I have a problem with strings saved in a database, like this for example: "311\315_316\336_337"
. They have only one backslash and this is a problem in java. When I try to s.replaceAll("\", "\\")
it doesn't work because the java compiler reads the string "311\315_316\336_337"
as "311Í_316Þ_337"
.
我也尝试编码和解码编码"311%C3%8D_316%C3%9E_337"
的结果,但是解码结果仍然是311Í_316Þ_337"
?
I also try encoding and decoding the result of encoding "311%C3%8D_316%C3%9E_337"
but the result of decoding is still "311Í_316Þ_337"
?
如何解决一个反斜杠的问题?
How can I solve problem of one backslash?
我想将此字符串传递给函数以执行拆分数字的过程,而反斜杠是分隔符,但是此问题使我无法这样做.
I want to pass this string to function to do a process which splits numbers and the backslash is the delimiter, but this problem prevents me from doing that.
推荐答案
数据库中的数据正常,您无需替换任何内容.直接用Java代码编写的字符串 literals 必须使它们的反斜杠被另一个反斜杠转义:
The data in the database is OK, and you don't have to replace anything. String literals, written directly in the Java code, must have their backslash escaped by another backslash:
String s = "311\\315_316\\336_337";
System.out.println(s); // prints 311\315_316\336_337
但是,如果您从数据库中获得了这些值,那么您就无事可做:
But if you get those values from the database, you don't have anything to do:
String s = resultSet.getString(1);
System.out.println(s); // should print 311\315_316\336_337
这篇关于Java中的反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!