Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split function difference between char and string arguments

Tags:

split

scala

I try the following code in scala REPL:

"ASD-ASD.KZ".split('.')
res7: Array[String] = Array(ASD-ASD, KZ)

"ASD-ASD.KZ".split(".")
res8: Array[String] = Array()

Why this function calls have a different results?

like image 523
Slow Harry Avatar asked Jun 08 '16 08:06

Slow Harry


People also ask

What is split () function in string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How many arguments does split () take in Python?

Syntax. The split() function takes two parameters as an argument, i.e., separator and maxsplit.

How split string and function is used to perform that operation?

Python split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.

What is the split function doing to the data?

The SPLIT( ) function breaks character data into segments based on separators such as spaces or commas and returns a specified segment.


1 Answers

There's a big difference in the function use.

The split function is overloaded, and this is the implementation from the source code of Scala:

/** For every line in this string:

  • Strip a leading prefix consisting of blanks or control characters
  • followed by | from the line.

*/

  def stripMargin: String = stripMargin('|')

  private def escape(ch: Char): String = "\\Q" + ch + "\\E"

  @throws(classOf[java.util.regex.PatternSyntaxException])
  def split(separator: Char): Array[String] = toString.split(escape(separator))

  @throws(classOf[java.util.regex.PatternSyntaxException])
  def split(separators: Array[Char]): Array[String] = {
    val re = separators.foldLeft("[")(_+escape(_)) + "]"
    toString.split(re)
  }

So when you're calling split() with a char, you ask to split by that specific char:

scala> "ASD-ASD.KZ".split('.')
res0: Array[String] = Array(ASD-ASD, KZ)

And when you're calling split() with a string, it means that you want to have a regex. So for you to get the exact result using the double quotes, you need to do:

scala> "ASD-ASD.KZ".split("\\.")
res2: Array[String] = Array(ASD-ASD, KZ)

Where:

  • First \ escapes the following character
  • Second \ escapes character for the dot which is a regex expression, and we want to use it as a character
  • . - the character to split the string by
like image 96
Avihoo Mamka Avatar answered Oct 14 '22 05:10

Avihoo Mamka