且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用点作为定界符?

更新时间:2023-02-20 09:22:01

扫描仪正在使用正则表达式(regex)作为分隔符和 是特殊字符,表示除行以外的任何字符因此,如果在编写 asdf时定界符是任何字符每个定界符都将被视为定界符,而不仅仅是点。您将使用 next()结果将是空字符串,该字符串存在于我标记为 |

Scanner is using regular expression (regex) as delimiter and dot . in regex is special character which represents any character except line separators. So if delimiter is any character when you write asdf. each of its character will be treated as delimiter, not only dot. So each time you will use next() result will be empty string which exists in places I marked with |

a|s|d|f|.

要创建点文字,您需要对其进行转义。您可以使用 \。。还有其他方法,例如使用字符类 [。]

To create dot literal you need to escape it. You can use \. for that. There are also other ways, like using character class [.].

所以尝试

input.useDelimiter("\\.");