如何将String转换为Java字符串文字? [英] How would you convert a String to a Java string literal?
本文介绍了如何将String转换为Java字符串文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是关于C#的这个问题。
假设我有一个 String
对象,我想在代码,我想生成一个映射到同一个东西的字符串文字。
Suppose I have a String
object which I want to represent in code and I want to produce a string literal that maps to the same thing.
示例
hello, "world"
goodbye
成为
hello, \"world\"\ngoodbye
我正准备编写一个状态机,逐个摄取字符串并适当地转义,但后来我想知道是否有更好的方法,或者是一个提供函数的库这样做。
I was just about to write a state machine that ingests the string character by character and escapes appropriately, but then I wondered if there was a better way, or a library that provides a function to do this.
推荐答案
如果你可以添加外部代码,Apache的Commons Lang有 EscapeStringUtils.escapeJava()
我认为这正是你想要的。
If you can add external code, Apache's Commons Lang has EscapeStringUtils.escapeJava()
which I think does exactly what you want.
这篇关于如何将String转换为Java字符串文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文