0

I have a case class like below:

 case class EmployeeJobDataFields( empID: String,firstName: String,lastName: 
  String,fullName: String)

And I have a data like this

    EmployeeJobDataFields("1043855", "Test", "User", "Test User")

To convert and obtain single string with pipe separator as shown below
 employee.productIterator.mkString("|")
 // val res0: String = 1043855|Test|User|Test User

 To convert string into a Array[String], I did split with pipe separator as shown below
 employee.productIterator.mkString("|").split("|")

I am expecting output as an array of strings like: Array(1043855, Test, User, Test User),

upon splitting I am getting output as shown below:

Array("1", "0", "4", "3", "8", "5", "5", "|", "T", "e", "s", "t", "|", "U", "s", "e", "r", "|"....)

Dev Kumar
  • 93
  • 7

1 Answers1

3

using single quotes it works:

 employee.productIterator.mkString("|").split('|')

String.split(char) is a scala method: https://www.scala-lang.org/api/current/scala/collection/StringOps.html#split(separator:Char):Array[String]

String.split(String) is a java method that sees the argument as a regex: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Since you are using the java version, "|" is interpreted as a regex. | in regex means or, hence it splits by anything (I'm not even sure | is a valid regex tbh)

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82