java 反斜杠 字符串,如何添加''\“反斜杠到字符串?

I have a

String a = "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?

解决方案

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:

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

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

...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[].

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值