I dont want to replace it with /u000A, do NOT want it to look like "asdxyz/u000A"
我不想用/ u000A替换它,不要让它看起来像“asdxyz / u000A”
I want to replace it with the actual newline CHARACTER.
我想用实际的换行符CHARACTER替换它。
3 个解决方案
#1
Based on your response to Natso's answer, it seems that you have a fundamental misunderstanding of what's going on. The two-character sequence \n
isn't the new-line character. But it's the way we represent that character in code because the actual character is hard to see. The compiler knows that when it encounters that two-character sequence in the context of a string literal, it should interpret them as the real new-line character, which has the ASCII value 10.
根据您对Natso答案的回应,您似乎对正在发生的事情存在根本性的误解。双字符序列\ n不是换行符。但这是我们在代码中表示该角色的方式,因为实际角色很难看到。编译器知道当它在字符串文字的上下文中遇到那个双字符序列时,它应该将它们解释为真正的换行符,其ASCII值为10。
If you print that two-character sequence to the console, you won't see them. Instead, you'll see the cursor advance to the next line. That's because the compiler has already replaced those two characters with the new-line character, so it's really the new-line character that got sent to the console.
如果您将两个字符的序列打印到控制台,您将看不到它们。相反,您将看到光标前进到下一行。那是因为编译器已经用换行符替换了这两个字符,所以它实际上是发送到控制台的新行字符。
If you have input to your program that contains backslashes and lowercase N's, and you want to convert them to new-line characters, then Zach's answer might be sufficient. But if you want your program to allow real backslashes in the input, then you'll need some way for the input to indicate that a backslash followed by a lowercase N is really supposed to be those two characters. The usual way to do that is to prefix the backslash with another backslash, escaping it. If you use Zach's code in that situation, you may end up turning the three-character sequence \\n
into the two-character sequence consisting of a backslash followed by a new-line character.
如果您的程序输入包含反斜杠和小写N,并且您想将它们转换为换行符,那么Zach的答案可能就足够了。但是如果你希望你的程序在输入中允许真正的反斜杠,那么你需要一些输入方式来指示反斜杠后跟一个小写的N真的应该是那两个字符。通常的方法是在反斜杠前加上另一个反斜杠,然后转义它。如果在这种情况下使用Zach的代码,最终可能会将三字符序列\ n转换为由反斜杠后跟换行字符组成的双字符序列。
The sure-fire way to read strings that use backslash escaping is to parse them one character at a time, starting from the beginning of the input. Copy characters from the input to the output, except when you encounter a backslash. In that case, check what the next character is, too. If it's another backslash, copy a single backslash to the output. If it's a lowercase N, then write a new-line character to the output. If it's any other character, so whatever you define to be the right thing. (Examples include rejecting the whole input as erroneous, pretending the backslash wasn't there, and omitting both the backslash and the following character.)
读取使用反斜杠转义的字符串的可靠方法是从输入的开头一次解析一个字符。将字符从输入复制到输出,除非遇到反斜杠。在这种情况下,也要检查下一个字符是什么。如果是另一个反斜杠,则将单个反斜杠复制到输出。如果它是小写N,则将新行字符写入输出。如果它是任何其他角色,那么无论你定义什么都是正确的。 (例子包括拒绝整个输入是错误的,假装反斜杠不存在,并省略反斜杠和后面的字符。)
If you're trying to observe the contents of a variable in the debugger, it's possible that the debugger may detect the new-line character convert it back to the two-character sequence \n
. So, if you're stepping through your code trying to figure out what's in it, you may fall victim to the debugger's helpfulness. And in most situations, it really is being helpful. Programmers usually want to know exactly what characters are in a string; they're less concerned about how those characters will appear on the screen.
如果您正在尝试在调试器中观察变量的内容,则调试器可能会检测到换行符将其转换回双字符序列\ n。因此,如果您正在逐步查看代码中的内容,那么您可能会成为调试器有用的牺牲品。在大多数情况下,它确实很有帮助。程序员通常想要确切地知道字符串中的字符是什么;他们不太关心这些角色在屏幕上的显示方式。
#3
Use the "(char)(10)" code to generate the true ascii value.
使用“(char)(10)”代码生成真正的ascii值。
newstr = oldstr.replaceAll("\\n",(char)(10));
// -or-
newstr = oldstr.replaceAll("\\n","" + ((char)(10)));
//(been a while)
//(已经有一段时间)
#1
Based on your response to Natso's answer, it seems that you have a fundamental misunderstanding of what's going on. The two-character sequence \n
isn't the new-line character. But it's the way we represent that character in code because the actual character is hard to see. The compiler knows that when it encounters that two-character sequence in the context of a string literal, it should interpret them as the real new-line character, which has the ASCII value 10.
根据您对Natso答案的回应,您似乎对正在发生的事情存在根本性的误解。双字符序列\ n不是换行符。但这是我们在代码中表示该角色的方式,因为实际角色很难看到。编译器知道当它在字符串文字的上下文中遇到那个双字符序列时,它应该将它们解释为真正的换行符,其ASCII值为10。
If you print that two-character sequence to the console, you won't see them. Instead, you'll see the cursor advance to the next line. That's because the compiler has already replaced those two characters with the new-line character, so it's really the new-line character that got sent to the console.
如果您将两个字符的序列打印到控制台,您将看不到它们。相反,您将看到光标前进到下一行。那是因为编译器已经用换行符替换了这两个字符,所以它实际上是发送到控制台的新行字符。
If you have input to your program that contains backslashes and lowercase N's, and you want to convert them to new-line characters, then Zach's answer might be sufficient. But if you want your program to allow real backslashes in the input, then you'll need some way for the input to indicate that a backslash followed by a lowercase N is really supposed to be those two characters. The usual way to do that is to prefix the backslash with another backslash, escaping it. If you use Zach's code in that situation, you may end up turning the three-character sequence \\n
into the two-character sequence consisting of a backslash followed by a new-line character.
如果您的程序输入包含反斜杠和小写N,并且您想将它们转换为换行符,那么Zach的答案可能就足够了。但是如果你希望你的程序在输入中允许真正的反斜杠,那么你需要一些输入方式来指示反斜杠后跟一个小写的N真的应该是那两个字符。通常的方法是在反斜杠前加上另一个反斜杠,然后转义它。如果在这种情况下使用Zach的代码,最终可能会将三字符序列\ n转换为由反斜杠后跟换行字符组成的双字符序列。
The sure-fire way to read strings that use backslash escaping is to parse them one character at a time, starting from the beginning of the input. Copy characters from the input to the output, except when you encounter a backslash. In that case, check what the next character is, too. If it's another backslash, copy a single backslash to the output. If it's a lowercase N, then write a new-line character to the output. If it's any other character, so whatever you define to be the right thing. (Examples include rejecting the whole input as erroneous, pretending the backslash wasn't there, and omitting both the backslash and the following character.)
读取使用反斜杠转义的字符串的可靠方法是从输入的开头一次解析一个字符。将字符从输入复制到输出,除非遇到反斜杠。在这种情况下,也要检查下一个字符是什么。如果是另一个反斜杠,则将单个反斜杠复制到输出。如果它是小写N,则将新行字符写入输出。如果它是任何其他角色,那么无论你定义什么都是正确的。 (例子包括拒绝整个输入是错误的,假装反斜杠不存在,并省略反斜杠和后面的字符。)
If you're trying to observe the contents of a variable in the debugger, it's possible that the debugger may detect the new-line character convert it back to the two-character sequence \n
. So, if you're stepping through your code trying to figure out what's in it, you may fall victim to the debugger's helpfulness. And in most situations, it really is being helpful. Programmers usually want to know exactly what characters are in a string; they're less concerned about how those characters will appear on the screen.
如果您正在尝试在调试器中观察变量的内容,则调试器可能会检测到换行符将其转换回双字符序列\ n。因此,如果您正在逐步查看代码中的内容,那么您可能会成为调试器有用的牺牲品。在大多数情况下,它确实很有帮助。程序员通常想要确切地知道字符串中的字符是什么;他们不太关心这些角色在屏幕上的显示方式。
#2
#3
Use the "(char)(10)" code to generate the true ascii value.
使用“(char)(10)”代码生成真正的ascii值。
newstr = oldstr.replaceAll("\\n",(char)(10));
// -or-
newstr = oldstr.replaceAll("\\n","" + ((char)(10)));
//(been a while)
//(已经有一段时间)