I'm making a Java program that takes some text as input,
and has to produce the equivalent JavaFX code (a String literal). For instance:
我正在制作一个Java程序,它将一些文本作为输入,并且必须生成等效的JavaFX代码(String文字)。例如:
The input is the following text:
输入内容如下:
Hello World! This: \ is a backslash. And this: {} are brackets.And the resulting JavaFX code is:
生成的JavaFX代码是:
"Hello World! This: \\ is a backslash.\nAnd this: \{\} are brackets."
Is there any native way (for example, using JavaFX SDKs) to achieve this?
If not, can someone give me the complete escaped sequences list in JavaFX?
是否有任何本地方式(例如,使用JavaFX SDK)来实现这一目标?如果没有,有人可以在JavaFX中给我完整的转义序列表吗?
1 个解决方案
#1
0
According to the JavaFX specification, the only characters that you have to escape with a backslash when using double quotation marks are:
根据JavaFX规范,使用双引号时必须使用反斜杠转义的唯一字符是:
"
{
}
\
Here's a Java method that should do what you're looking for:
这是一个Java方法,应该做你正在寻找的:
public String escapeInput(String[] input) {
String[] characters = {"\"", "\\", "{", "}"};
StringBuilder sb = new StringBuilder();
sb.append("\"");
for (String line : input) {
for (String test : characters) {
line = line.replace(test, "\\" + test);
}
sb.append(line);
sb.append("\n");
}
sb.append("\"");
return sb.toString();
}
#1
0
According to the JavaFX specification, the only characters that you have to escape with a backslash when using double quotation marks are:
根据JavaFX规范,使用双引号时必须使用反斜杠转义的唯一字符是:
"
{
}
\
Here's a Java method that should do what you're looking for:
这是一个Java方法,应该做你正在寻找的:
public String escapeInput(String[] input) {
String[] characters = {"\"", "\\", "{", "}"};
StringBuilder sb = new StringBuilder();
sb.append("\"");
for (String line : input) {
for (String test : characters) {
line = line.replace(test, "\\" + test);
}
sb.append(line);
sb.append("\n");
}
sb.append("\"");
return sb.toString();
}