简体   繁体   English

与String.split的奇怪操作与用户输入不同

[英]Strange Operating with String.split differently with user input

I can not get my head around this. 我无法解决这个问题。

System.err.println(convertPdfToText(save).split("3\\.8").length);

as expected prints out a result of 3. 按预期打印出结果3。

In fact, I want a user to be able to obtain the same. 实际上,我希望用户能够获得相同的信息。 Therefore, when I run this code: 因此,当我运行此代码时:

System.err.println(convertPdfToText(save).split(seperator).length);

When the user has entered "3\\\\.8" in a JInputField, I get the length of the array being returned as 1. 当用户在JInputField中输入“ 3 \\ .. 8”时,得到的数组长度为1。

What exactly is happening here? 这里到底发生了什么?

"\\\\" in a Java program represents a string containing one \\ . Java程序中的"\\\\"表示包含一个\\的字符串。 In your case, the user would have to enter 3\\.8 to get the string represented by the Java code "3\\\\.8" . 在您的情况下,用户必须输入3\\.8才能获得由Java代码"3\\\\.8"表示的字符串。

That is, you seem to be confused by the difference between Java's syntax for string literals (which treats \\ and " specially) and strings entered from external sources (where every character stands for itself). 也就是说,您似乎对Java字符串文字的语法(专门处理\\" )与从外部源(每个字符代表其自身)输入的字符串之间的差异感到困惑。

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

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