Why does Split behave differently on different lines?

Here are two cases:

Case 1:

scala> "".split('f')
res3: Array[String] = Array("")

Case 2:

scala> "f".split('f')
res5: Array[String] = Array()

Why is everything different here? A specific explanation would be wonderful!

+4
source share
2 answers

In the first case, you provide a string and a separator that does not match any of the characters in this string. This way it just returns the original string. This can be illustrated by a non-empty string example:

scala> "abcd".split('f')
res2: Array[String] = Array(abcd)

However, your second line contains only the delimiter. Thus, it matches the delimiter and breaks the string. Since splits do not contain anything, it returns an empty array. According to Java String docs:

:

- , , .

:

, .

: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String,%20int)

+9

split, , String, String, .

0

Source: https://habr.com/ru/post/1658666/


All Articles