简体   繁体   English

Java String Split() 方法

[英]Java String Split() Method

I was wondering what the following line would do:我想知道以下行会做什么:

String parts = inputLine.split("\\s+");

Would this simply split the string at any spaces in the line?这会简单地在行中的任何空格处拆分字符串吗? I think this a regex, but I've never seen them before.我认为这是一个正则表达式,但我以前从未见过它们。

Yes, as documentation states split takes regex as argument.是的,因为文档指出split将正则表达式作为参数。

In regex \\s represents character class of containing whitespace characters like:在正则表达式中\\s表示包含空格字符的字符类,例如:

  • tab \\t ,标签\\t ,
  • space " " ,空格" " ,
  • line separators \\n \\r行分隔符\\n \\r
  • more...更多的...

+ is quantifier which can be read as " once or more " which makes \\s+ representing text build from one or more whitespaces. +量词,可以读作“一次或多次”,这使得\\s+表示从一个或多个空格构建的文本。

We need to write this regex as "\\\\s+ (with two backslashes) because in String \\ is considered special character which needs escaping (with another backslash) to produce \\ literal.我们需要将此正则表达式写为"\\\\s+ (带有两个反斜杠),因为在字符串中\\被认为是需要转义(带有另一个反斜杠)以生成\\文字的特殊字符。

So split("\\\\s+") will produce array of tokens separated by one or more whitespaces.所以split("\\\\s+")将产生由一个或多个空格分隔的标记数组。 BTW trailing empty elements are removed so "abc ".split("\\\\s+") will return array ["a", "b", "c"] not ["a", "b", "c", ""] . BTW 尾随空元素被删除,所以"abc ".split("\\\\s+")将返回数组["a", "b", "c"]而不是["a", "b", "c", ""]

Yes, though actually any number of space meta-characters (including tabs, newlines etc).是的,尽管实际上是任意数量的空格元字符(包括制表符、换行符等)。 See the Java documentation on Patterns .请参阅有关 PatternsJava 文档

It will split the string on one (or more) consecutive white space characters.它会将字符串拆分为一个(或多个)连续的空白字符。 The Pattern Javadoc describes the Predefined character classes (of which \\s is one) as, Pattern Javadoc将预定义字符类( \\s是其中之一)描述为,

Predefined character classes预定义的字符类

. Any character (may or may not match line terminators) \\d A digit: [0-9] \\DA non-digit: [^0-9] \\s A whitespace character: [ \\t\\n\\x0B\\f\\r] \\SA non-whitespace character: [^\\s] \\w A word character: [a-zA-Z_0-9] \\WA non-word character: [^\\w]

Note that the \\\\ is to escape the back-slash as required to embed it in a String .请注意, \\\\是根据需要转义反斜杠以将其嵌入String

Yes, and it splits both tab and space:是的,它同时拆分了制表符和空格:

String t = "test your   function      aaa";

for(String s : t.split("\\s+"))
   System.out.println(s);

Output:输出:

 test your function aaa

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

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