用点作为分隔符拆分字符串

时间:2022-11-28 21:39:23

I am wondering if I am going about splitting a string on a . the right way? My code is:

我想知道我是否要在一个字符串上拆分。正确的方式?我的代码是:

String[] fn = filename.split(".");
return fn[0];

I only need the first part of the string, that's why I return the first item. I ask because I noticed in the API that . means any character, so now I'm stuck.

我只需要字符串的第一部分,这就是我返回第一项的原因。我问,因为我在API中注意到了。意味着任何角色,所以现在我被卡住了。

9 个解决方案

#1


129  

split() accepts a regular expression, so you need to escape . to not consider it as a regex meta character. Here's an exemple :

split()接受正则表达式,因此您需要转义。不要将它视为正则表达式元字符。这是一个例子:

String[] fn = filename.split("\\."); 
return fn[0];

#2


15  

Split uses regular expressions, where '.' is a special character meaning anything. You need to escape it if you actually want it to match the '.' character:

Split使用正则表达式,其中'。'是一个特殊的角色意味着什么。如果你真的希望它与''匹配,你需要逃脱它。字符:

String[] fn = filename.split("\\.");

(one '\' to escape the '.' in the regular expression, and the other to escape the first one in the Java string)

(一个'\'在正则表达式中转义'。',另一个转义为转义Java字符串中的第一个'。'

Also I wouldn't suggest returning fn[0] since if you have a file named something.blabla.txt, which is a valid name you won't be returning the actual file name. Instead I think it's better if you use:

另外我不建议返回fn [0],因为如果你有一个名为something.blabla.txt的文件,这是一个有效名称,你将不会返回实际的文件名。相反,我认为如果你使用它会更好:

int idx = filename.lastIndexOf('.');
return filename.subString(0, idx);

#3


13  

the String#split(String) method uses regular expressions. In regular expressions, the "." character means "any character". You can avoid this behavior by either escaping the "."

String#split(String)方法使用正则表达式。在正则表达式中,“。”字符表示“任何字符”。您可以通过转义“。”来避免此行为。

filename.split("\\.");

or telling the split method to split at at a character class:

或者告诉split方法在一个字符类中拆分:

filename.split("[.]");

Character classes are collections of characters. You could write

字符类是字符集合。你可以写

filename.split("[-.;ld7]");

and filename would be split at every "-", ".", ";", "l", "d" or "7". Inside character classes, the "." is not a special character ("metacharacter").

和文件名将在每个“ - ”,“。”,“;”,“l”,“d”或“7”分开。里面的字符类,“。”不是特殊字符(“元字符”)。

#4


9  

I see only solutions here but no full explanation of the problem so I decided to post this answer

我在这里只看到解决方案,但没有对问题的完整解释,所以我决定发布这个答案

Problem

You need to know few things about text.split(delim). split method:

你需要了解一些关于text.split(delim)的东西。拆分方法:

  1. accepts as argument regular expression (regex) which describes delimiter on which we want to split,
  2. 接受作为参数的正则表达式(regex),它描述了我们想要拆分的分隔符,
  3. if delim exists at end of text like in a,b,c,, (where delimiter is ,) split at first will create array like ["a" "b" "c" "" ""] but since in most cases we don't really need these trailing empty strings it also removes them automatically for us. So it creates another array without these trailing empty strings and returns it.
  4. 如果delim存在于文本的末尾,如a,b,c ,,(其中分隔符是),则首先分割将创建类似[“a”“b”“c”“”“”]的数组但是因为在大多数情况下我们不需要这些尾随的空字符串,它也会自动删除它们。所以它创建了另一个没有这些尾随空字符串的数组并返回它。

You also need to know that dot . is special character in regex. It represents any character (except line separators but this can be changed with Pattern.DOTALL flag).

你还需要知道那个点。是正则表达式中的特殊字符。它表示任何字符(行分隔符除外,但可以使用Pattern.DOTALL标志更改)。

So for string like "abc" if we split on "." split method will

因此对于像“abc”这样的字符串,如果我们分开“。”分裂方法会

  1. create array like ["" "" "" ""],
  2. 创建像[“”“”“”“”“这样的数组,
  3. but since this array contains only empty strings and they all are trailing they will be removed (like shown in previous second point)
  4. 但是因为这个数组只包含空字符串而且它们都是尾随字符,所以它们将被删除(如前面第二点所示)

which means we will get as result empty array [] (with no elements, not even empty string), so we can't use fn[0] because there is no index 0.

这意味着我们将得到结果为空数组[](没有元素,甚至没有空字符串),所以我们不能使用fn [0],因为没有索引0。

Solution

To solve this problem you simply need to create regex which will represents dot. To do so we need to escape that .. There are few ways to do it, but simplest is probably by using \ (which in String needs to be written as "\\" because \ is also special there and requires another \ to be escaped).

要解决这个问题,您只需要创建代表点的正则表达式。要做到这一点,我们需要逃避它..有几种方法可以做到,但最简单的可能是使用\(在String中需要写为“\\”,因为\也是特殊的,需要另一个\来转义)。

So solution to your problem may look like

因此,您的问题的解决方案可能会像

String[] fn = filename.split("\\.");

Bonus

奖金

You can also use other ways to escape that dot like

您也可以使用其他方式来逃避该点

  • using character class split("[.]")
  • 使用字符类拆分(“[。]”)
  • wrapping it in quote split("\\Q.\\E")
  • 将它包装在引用拆分中(“\\ Q。\\ E”)
  • using proper Pattern instance with Pattern.LITERAL flag
  • 使用带有Pattern.LITERAL标志的正确Pattern实例
  • or simply use split(Pattern.quote(".")) and let regex do escaping for you.
  • 或者只是使用split(Pattern.quote(“。”))并让正则表达式为你逃脱。

#5


5  

As DOT( . ) is considered as a special character and split method of String expects a regular expression you need to do like this -

由于DOT(。)被认为是一个特殊字符,String的split方法需要一个正则表达式,你需要这样做 -

String[] fn = filename.split("\\.");
return fn[0];

In java the special characters need to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" !

在java中,特殊字符需要使用“\”进行转义,但由于“\”也是Java中的特殊字符,因此需要使用另一个“\”再次转义它!

#6


2  

Wouldn't it be more efficient to use

使用它不是更有效率

 filename.substring(0, filename.indexOf("."))

if you only want what's up to the first dot?

如果你只想要第一个点?

#7


2  

Usually its NOT a good idea to unmask it by hand. There is a method in the Pattern class for this task:

通常手动揭开它并不是一个好主意。 Pattern类中有一个方法可用于此任务:

java.util.regex
static String quote(String s) 

#8


1  

The split must be taking regex as a an argument... Simply change "." to "\\."

分裂必须以正则表达式为参数......只需改变“。”至 ”\\。”

#9


-1  

split takes a regex as argument. So you should pass "\." instead of "." because "." is a metacharacter in regex.

split以正则表达式作为参数。所以你应该通过“\”。代替 ”。”因为“。”是正则表达式中的元字符。

#1


129  

split() accepts a regular expression, so you need to escape . to not consider it as a regex meta character. Here's an exemple :

split()接受正则表达式,因此您需要转义。不要将它视为正则表达式元字符。这是一个例子:

String[] fn = filename.split("\\."); 
return fn[0];

#2


15  

Split uses regular expressions, where '.' is a special character meaning anything. You need to escape it if you actually want it to match the '.' character:

Split使用正则表达式,其中'。'是一个特殊的角色意味着什么。如果你真的希望它与''匹配,你需要逃脱它。字符:

String[] fn = filename.split("\\.");

(one '\' to escape the '.' in the regular expression, and the other to escape the first one in the Java string)

(一个'\'在正则表达式中转义'。',另一个转义为转义Java字符串中的第一个'。'

Also I wouldn't suggest returning fn[0] since if you have a file named something.blabla.txt, which is a valid name you won't be returning the actual file name. Instead I think it's better if you use:

另外我不建议返回fn [0],因为如果你有一个名为something.blabla.txt的文件,这是一个有效名称,你将不会返回实际的文件名。相反,我认为如果你使用它会更好:

int idx = filename.lastIndexOf('.');
return filename.subString(0, idx);

#3


13  

the String#split(String) method uses regular expressions. In regular expressions, the "." character means "any character". You can avoid this behavior by either escaping the "."

String#split(String)方法使用正则表达式。在正则表达式中,“。”字符表示“任何字符”。您可以通过转义“。”来避免此行为。

filename.split("\\.");

or telling the split method to split at at a character class:

或者告诉split方法在一个字符类中拆分:

filename.split("[.]");

Character classes are collections of characters. You could write

字符类是字符集合。你可以写

filename.split("[-.;ld7]");

and filename would be split at every "-", ".", ";", "l", "d" or "7". Inside character classes, the "." is not a special character ("metacharacter").

和文件名将在每个“ - ”,“。”,“;”,“l”,“d”或“7”分开。里面的字符类,“。”不是特殊字符(“元字符”)。

#4


9  

I see only solutions here but no full explanation of the problem so I decided to post this answer

我在这里只看到解决方案,但没有对问题的完整解释,所以我决定发布这个答案

Problem

You need to know few things about text.split(delim). split method:

你需要了解一些关于text.split(delim)的东西。拆分方法:

  1. accepts as argument regular expression (regex) which describes delimiter on which we want to split,
  2. 接受作为参数的正则表达式(regex),它描述了我们想要拆分的分隔符,
  3. if delim exists at end of text like in a,b,c,, (where delimiter is ,) split at first will create array like ["a" "b" "c" "" ""] but since in most cases we don't really need these trailing empty strings it also removes them automatically for us. So it creates another array without these trailing empty strings and returns it.
  4. 如果delim存在于文本的末尾,如a,b,c ,,(其中分隔符是),则首先分割将创建类似[“a”“b”“c”“”“”]的数组但是因为在大多数情况下我们不需要这些尾随的空字符串,它也会自动删除它们。所以它创建了另一个没有这些尾随空字符串的数组并返回它。

You also need to know that dot . is special character in regex. It represents any character (except line separators but this can be changed with Pattern.DOTALL flag).

你还需要知道那个点。是正则表达式中的特殊字符。它表示任何字符(行分隔符除外,但可以使用Pattern.DOTALL标志更改)。

So for string like "abc" if we split on "." split method will

因此对于像“abc”这样的字符串,如果我们分开“。”分裂方法会

  1. create array like ["" "" "" ""],
  2. 创建像[“”“”“”“”“这样的数组,
  3. but since this array contains only empty strings and they all are trailing they will be removed (like shown in previous second point)
  4. 但是因为这个数组只包含空字符串而且它们都是尾随字符,所以它们将被删除(如前面第二点所示)

which means we will get as result empty array [] (with no elements, not even empty string), so we can't use fn[0] because there is no index 0.

这意味着我们将得到结果为空数组[](没有元素,甚至没有空字符串),所以我们不能使用fn [0],因为没有索引0。

Solution

To solve this problem you simply need to create regex which will represents dot. To do so we need to escape that .. There are few ways to do it, but simplest is probably by using \ (which in String needs to be written as "\\" because \ is also special there and requires another \ to be escaped).

要解决这个问题,您只需要创建代表点的正则表达式。要做到这一点,我们需要逃避它..有几种方法可以做到,但最简单的可能是使用\(在String中需要写为“\\”,因为\也是特殊的,需要另一个\来转义)。

So solution to your problem may look like

因此,您的问题的解决方案可能会像

String[] fn = filename.split("\\.");

Bonus

奖金

You can also use other ways to escape that dot like

您也可以使用其他方式来逃避该点

  • using character class split("[.]")
  • 使用字符类拆分(“[。]”)
  • wrapping it in quote split("\\Q.\\E")
  • 将它包装在引用拆分中(“\\ Q。\\ E”)
  • using proper Pattern instance with Pattern.LITERAL flag
  • 使用带有Pattern.LITERAL标志的正确Pattern实例
  • or simply use split(Pattern.quote(".")) and let regex do escaping for you.
  • 或者只是使用split(Pattern.quote(“。”))并让正则表达式为你逃脱。

#5


5  

As DOT( . ) is considered as a special character and split method of String expects a regular expression you need to do like this -

由于DOT(。)被认为是一个特殊字符,String的split方法需要一个正则表达式,你需要这样做 -

String[] fn = filename.split("\\.");
return fn[0];

In java the special characters need to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" !

在java中,特殊字符需要使用“\”进行转义,但由于“\”也是Java中的特殊字符,因此需要使用另一个“\”再次转义它!

#6


2  

Wouldn't it be more efficient to use

使用它不是更有效率

 filename.substring(0, filename.indexOf("."))

if you only want what's up to the first dot?

如果你只想要第一个点?

#7


2  

Usually its NOT a good idea to unmask it by hand. There is a method in the Pattern class for this task:

通常手动揭开它并不是一个好主意。 Pattern类中有一个方法可用于此任务:

java.util.regex
static String quote(String s) 

#8


1  

The split must be taking regex as a an argument... Simply change "." to "\\."

分裂必须以正则表达式为参数......只需改变“。”至 ”\\。”

#9


-1  

split takes a regex as argument. So you should pass "\." instead of "." because "." is a metacharacter in regex.

split以正则表达式作为参数。所以你应该通过“\”。代替 ”。”因为“。”是正则表达式中的元字符。