gpt4 book ai didi

java - 解释 Java 字符串

转载 作者:行者123 更新时间:2023-11-29 06:19:12 24 4
gpt4 key购买 nike

有什么方法可以将诸如“hello\r\n\world”之类的字符串解释为一个字符串,其中\r\n 已转换为它们的实际文字值。

场景是用户键入正则表达式替换表达式并在 UI 中键入\r\n。该 get 已转义,但我想从中获取实际解释的字符串。

最佳答案

我并不直接熟悉处理此问题的“简单”方法(即我注意到是否有可以处理此问题的内置库)。但是这样做的一种方法是阅读关于转义序列的 JLS 规范并编写一个单 channel 解析器,它可以找到并评估每个转义序列。 (检查 JLS 3.10.6 Escape Sequences for Character and String Literals )。

现在我知道了一个事实,有一些你很快就会忘记处理的怪事,例如八进制转义很棘手,因为它允许 1,2 或 3 位数字,并且在每种情况下允许不同的当它是转义时的值,当它只是整数时的值。

以字符串“\431”为例,这是与字符“1”连接的局部转义符“\43”,因为八进制转义符的第一位数字是 4,因此不能是完整的三位八进制值在这种情况下,它只允许 [0-3] 作为第一个数字。

就在大约一年前,我正在为 1.3 规范的一个子集共同编写一个 Java 编译器,它确实有转义序列,下面我包含了我们处理转义的代码——你实际上应该能够接受这个按原样编写代码并包含在 Utility 类中(如果您觉得仁慈,可以加入功劳):

private String processCharEscapes(String strVal) {
// Loop helpers
char[] chrArr = strVal.toCharArray();
StringBuilder strOut = new StringBuilder(strVal.length());
String strEsc = ""; // Escape sequence, string buffer
Character chrBuf = null; // Dangling character buffer

// Control flags
boolean inEscape = false; // In escape?
boolean cbOctal3 = true; // Can be octal 3-digit

// Parse characters
for(char c : chrArr) {
if (!inEscape) {
// Listen for start of escape sequence
if (c == '\\') {
inEscape = true; // Enter escape
strEsc = ""; // Reset escape buffer
chrBuf = null; // Reset dangling character buffer
cbOctal3 = true; // Reset cbOctal3 flag
} else {
strOut.append(c); // Save to output
}
} else {
// Determine escape termination
if (strEsc.length() == 0) { // First character
if (c >= 48 && c <= 55) { // c is a digit [0-7]
if (c > 51) { // c is a digit [4-7]
cbOctal3 = false;
}
strEsc += c; // Save to buffer
} else { // c is a character
// Single-character escapes (will terminate escape loop)
if (c == 'n') {
inEscape = false;
strOut.append('\n');
} else if(c == 't') {
inEscape = false;
strOut.append('\t');
} else if(c == 'b') {
inEscape = false;
strOut.append('\b');
} else if(c == 'r') {
inEscape = false;
strOut.append('\r');
} else if(c == 'f') {
inEscape = false;
strOut.append('\f');
} else if(c == '\\') {
inEscape = false;
strOut.append('\\');
} else if(c == '\'') {
inEscape = false;
strOut.append('\'');
} else if(c == '"') {
inEscape = false;
strOut.append('"');
} else {
// Saw illegal character, after escape character '\'
System.err.println(ErrorType.SYNTAX_ERROR, "Illegal character escape sequence, unrecognised escape: \\" + c);
}
}
} else if(strEsc.length() == 1) { // Second character (possibly)
if (c >= 48 && c <= 55) { // c is a digit [0-7]
strEsc += c; // Save to buffer
if (!cbOctal3) { // Terminate since !cbOctal3
inEscape = false;
}
} else {
inEscape = false; // Terminate since c is not a digit
chrBuf = c; // Save dangling character
}
} else if(strEsc.length() == 2) { // Third character (possibly)
if (cbOctal3 && c >= 48 && c <= 55) {
strEsc += c; // Save to buffer
} else {
chrBuf = c; // Save dangling character
}
inEscape = false; // Will always terminate after third character, no matter what
}

// Did escape sequence terminate, at character c?
if (!inEscape && strEsc.length() > 0) {
// strEsc is legal 1-3 digit octal char code, convert and add
strOut.append((char)Integer.parseInt(strEsc, 8));

if (chrBuf != null) { // There was a dangling character
// Check for chained escape sequences (e.g. \10\10)
if (chrBuf == '\\') {
inEscape = true; // Enter escape
strEsc = ""; // Reset escape buffer
chrBuf = null; // Reset dangling character buffer
cbOctal3 = true; // Reset cbOctal3 flag
} else {
strOut.append(chrBuf);
}
}
}
}
}

// Check for EOL-terminated escape sequence (special case)
if (inEscape) {
// strEsc is legal 1-3 digit octal char code, convert and add
strOut.append((char)Integer.parseInt(strEsc, 8));

if (chrBuf != null) { // There was a dangling character
strOut.append(chrBuf);
}
}

return strOut.toString();
}

希望对你有帮助

关于java - 解释 Java 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3843591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com