Java 用点作为分隔符分割字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3387622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split string with dot as delimiter
提问by Dean
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 中注意到这.
意味着任何字符,所以现在我被卡住了。
采纳答案by Marimuthu Madasamy
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];
回答by Bob Fincheimer
The split must be taking regex as a an argument... Simply change "."
to "\\."
拆分必须将正则表达式作为参数......只需更改"."
为"\\."
回答by Vijay Mathew
split
takes a regex as argument. So you should pass "\."
instead of "."
because "."
is a metacharacter in regex.
split
将正则表达式作为参数。所以你应该通过"\."
而不是"."
因为"."
是正则表达式中的元字符。
回答by f1sh
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”处拆分。在字符类中,“.” 不是特殊字符(“元字符”)。
回答by Martin Smith
Wouldn't it be more efficient to use
使用会不会更有效率
filename.substring(0, filename.indexOf("."))
if you only want what's up to the first dot?
如果你只想要第一个点是什么?
回答by Andrei Fierbinteanu
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);
回答by Neel
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中也是一个特殊字符,你需要用另一个“\”再次转义它!
回答by Christian Ullenboom
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)
回答by Pshemo
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)
. split
方法:
- accepts as argument regular expression(regex) which describes delimiter on which we want to split,
- if
delim
exists at end oftext
like ina,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.
- 接受作为参数的正则表达式(regex),它描述了我们想要分割的分隔符,
- 如果
delim
在text
like ina,b,c,,
(分隔符所在的位置,
)的末尾存在,split
首先会创建数组 like["a" "b" "c" "" ""]
但由于在大多数情况下我们并不真正需要这些尾随空字符串,它也会自动为我们删除它们。所以它创建另一个没有这些尾随空字符串的数组并返回它。
You also need to know that dot .
is special characterin 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"
如果我们在"."
split
方法上拆分将
- create array like
["" "" "" ""]
, - but since this array contains only empty strings and they all are trailing they will be removed (like shown in previous second point)
- 创建数组,如
["" "" "" ""]
, - 但是由于这个数组只包含空字符串并且它们都在尾随它们将被删除(如前第二点所示)
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")
- using proper Pattern instance with
Pattern.LITERAL
flag - or simply use
split(Pattern.quote("."))
and let regex do escaping for you.
- 使用字符类
split("[.]")
- 用引号包裹它
split("\\Q.\\E")
- 使用带有
Pattern.LITERAL
标志的正确 Pattern 实例 - 或者简单地使用
split(Pattern.quote("."))
并让正则表达式为您转义。
回答by avl42
Note: Further care should be taken with this snippet, even after the dot is escaped!
注意:即使在转义点之后,也应进一步注意此代码段!
If filename is just the string ".", then fn will still end up to be of 0 length and fn[0] will still throw an exception!
如果文件名只是字符串“.”,那么 fn 最终仍然是 0 长度并且 fn[0] 仍然会抛出异常!
This is, because if the pattern matches at least once, then split will discard all trailing empty strings(thus also the one beforethe dot!) from the array, leaving an empty array to be returned.
这是因为如果模式至少匹配一次,那么 split 将从数组中丢弃所有尾随的空字符串(因此也是点之前的一个!),留下一个空数组返回。