如何添加''\“反斜杠到字符串? [英] How to add ''\" Backslash to a String?

查看:2134
本文介绍了如何添加''\“反斜杠到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

String a = "51109965.xml";

我需要添加此字符:'\'。所以最后我会得到一个新字符串\ 51109965.xml

I need to add this character: '\'. So at the end I will get a new string "\51109965.xml".

我正在尝试使用此命令:

I am trying to use this command:

StringBuilder PathBufferXML_new_Provider = new StringBuilder().append("\").append("51109965").append(".xml");  

但因为此符号'\'保存,我收到一个错误。
如何解决这个问题?

But because this sign '\' is saved, I get an error. How can I solve this issue?

推荐答案

在Java中, \ 是一个转义字符。这意味着,它逃脱了以下标志的原始含义。例如,当想要打印具有特殊含义的时,他可以写 System.out.println(\); 。让我们试着找到你的问题的答案,可以重构为:

In Java, \ is an escape character. It means, that it escapes the original meaning of the following sign. For example, when one wants to print " which has a special meaning, he can write System.out.println("\"");. Let's try to find the answer to your question which could be refactored to:


如何打印 \ 在Java中,因为它是一个从特殊字符中逃脱特殊含义的特殊字符?

How can I print \ in Java, as it's a special character which escapes special meaning from the special characters?

System.out.println(\\); 将是您正在寻找的内容,因为它已转义为 \ character。

System.out.println("\\"); will be what you're looking for as it's escaped speacial meaning of a \ character.

...并且非常重要:Java中的字符用单引号表示:'\'而不是\表示 String 对象,其对象的私有字段为 char []

...and a very important thing: characters in Java are represented in single quotes: '\' instead of "\" which represents a String object which has a private field of type char[].

这篇关于如何添加''\“反斜杠到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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