简体   繁体   English

Java将转义序列添加到字符串

[英]Java Add Escape Sequence to String

Say for example, that I have a method that returns the string \\"ex\\". 举例来说,我有一个返回字符串\\“ ex \\”的方法。 Obviously, this will be printed out as "ex" 显然,这将被打印为“ ex”

How can I make a method that allows me to print \\"ex\\" without the escape sequences cut out? 如何制作一种允许我在不切出转义序列的情况下打印\\“ ex \\”的方法? I need to create or use another method to do so, as I have no access to the original method that returns the string. 我需要创建或使用其他方法来执行此操作,因为我无法访问返回字符串的原始方法。

The reason I'm doing this is because I am trying to create a JSON representation to parse with a json parser. 我这样做的原因是因为我试图创建JSON表示形式以使用JSON解析器进行解析。 With my current code, I am getting 用我当前的代码,我得到

{example:[{"url":""ex""}]} along with all the other special characters that individually need an escape character in order for it to parse properly. {example:[{“ URL”:“” ex“​​”}]}以及所有其他需要单独使用转义字符才能正确解析的特殊字符。

use this statement: 使用此语句:

return "\\\"ex\"\\";

this will be printed:\\"ex"\\ or: 它将被打印:\\“ ex” \\或:

return "\\ex\\"

this will be printed:\\ex\\ 这将被打印:\\ ex \\

You dont have to worry about the / You can do something like 您不必担心/您可以做类似的事情

System.out.println("http://google.com");

This will work fine as / is not a special character in java. 由于/不是java中的特殊字符,因此可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM