使用带小数的string.split() - 不起作用

时间:2021-07-31 16:35:00

I'm trying to split a line of text into multiple parts. Each element of the text is separated by a period. I'm using string.split("."); to split the text into an array of Strings, but not getting anywhere.

我正在尝试将一行文本分成多个部分。文本的每个元素由句点分隔。我正在使用string.split(“。”);将文本拆分为一个字符串数组,但没有到达任何地方。

Here is a sample of the code:

以下是代码示例:

String fileName = "testing.one.two";

String[] fileNameSplit = fileName.split(".");

System.out.println(fileNameSplit[0]);

The funny thing is, when I try ":" instead of ".", it works? How can I get it to work for a period?

有趣的是,当我尝试“:”而不是“。”时,它有效吗?我怎样才能让它工作一段时间?

6 个解决方案

#1


27  

String.split() accepts a regular expression (regex for short) and dot is a special char in regexes. It means "match all chars except newlines". So you must escape it with a leading backslash. But the leading backslash is a special character in java string literals. It denotes an escape sequence. So it must be escaped too, with another leading backslash. Like this:

String.split()接受正则表达式(简称正则表达式),dot是正则表达式中的特殊字符。这意味着“匹配除换行符之外的所有字符”。因此,您必须使用前导反斜杠来逃避它。但是前导反斜杠是java字符串文字中的一个特殊字符。它表示转义序列。所以它必须也被逃脱,另一个领先的反斜杠。喜欢这个:

fileName.split("\\.");

#2


6  

Try this one: fileName.split("\\.");

试试这个:fileName.split(“\\。”);

#3


3  

fileName.split(".");

should be

应该

fileName.split("\\.");

. is special character and split() accepts regex. So, you need to escape the special characters.

。是特殊字符,split()接受正则表达式。所以,你需要逃避特殊字符。

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. Please read this documentation.

以反斜杠(\)开头的字符是转义序列,对编译器具有特殊含义。请阅读本文档。

#4


3  

It's because the argument to split is a regular expression, and . means basically any character. Use "\\." instead of "." and it should work fine.

这是因为split的参数是一个正则表达式,而且。基本上是指任何角色。使用 ”\\。”代替 ”。”它应该工作正常。

The regular expression for a literal period (as opposed to the any-character .) is \. (using the \ escape character forces the literal interpretation).

文字句点的正则表达式(与任意字符相对)是\。 (使用\ escape字符强制字面解释)。

And, because it's within a string where \ already has special meaning, you need to escape that as well.

而且,因为它在\已经具有特殊含义的字符串中,你也需要逃避它。

#5


1  

You need to escape the "." character because split accept regular expressions and the . means any character, so for saying to the split method to split by point you must escape it like this:

你需要逃避“。”字符因为split接受正则表达式而且。意味着任何字符,所以为了说分割方法逐点拆分你必须像这样逃避它:

String[] array = string.split('\\.');

#6


0  

The split() takes in param a regex

split()接受param一个正则表达式

Try.using

Try.using

String[] fileNameSplit = fileName.split("\\.");

#1


27  

String.split() accepts a regular expression (regex for short) and dot is a special char in regexes. It means "match all chars except newlines". So you must escape it with a leading backslash. But the leading backslash is a special character in java string literals. It denotes an escape sequence. So it must be escaped too, with another leading backslash. Like this:

String.split()接受正则表达式(简称正则表达式),dot是正则表达式中的特殊字符。这意味着“匹配除换行符之外的所有字符”。因此,您必须使用前导反斜杠来逃避它。但是前导反斜杠是java字符串文字中的一个特殊字符。它表示转义序列。所以它必须也被逃脱,另一个领先的反斜杠。喜欢这个:

fileName.split("\\.");

#2


6  

Try this one: fileName.split("\\.");

试试这个:fileName.split(“\\。”);

#3


3  

fileName.split(".");

should be

应该

fileName.split("\\.");

. is special character and split() accepts regex. So, you need to escape the special characters.

。是特殊字符,split()接受正则表达式。所以,你需要逃避特殊字符。

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. Please read this documentation.

以反斜杠(\)开头的字符是转义序列,对编译器具有特殊含义。请阅读本文档。

#4


3  

It's because the argument to split is a regular expression, and . means basically any character. Use "\\." instead of "." and it should work fine.

这是因为split的参数是一个正则表达式,而且。基本上是指任何角色。使用 ”\\。”代替 ”。”它应该工作正常。

The regular expression for a literal period (as opposed to the any-character .) is \. (using the \ escape character forces the literal interpretation).

文字句点的正则表达式(与任意字符相对)是\。 (使用\ escape字符强制字面解释)。

And, because it's within a string where \ already has special meaning, you need to escape that as well.

而且,因为它在\已经具有特殊含义的字符串中,你也需要逃避它。

#5


1  

You need to escape the "." character because split accept regular expressions and the . means any character, so for saying to the split method to split by point you must escape it like this:

你需要逃避“。”字符因为split接受正则表达式而且。意味着任何字符,所以为了说分割方法逐点拆分你必须像这样逃避它:

String[] array = string.split('\\.');

#6


0  

The split() takes in param a regex

split()接受param一个正则表达式

Try.using

Try.using

String[] fileNameSplit = fileName.split("\\.");