scala> def listline(line: String) = for(i <- line.split(",").toList) println(i.trim)
listline: (line: String)Unit
scala> listline("abc,def,ghi")
abc
def
ghi
Update: More complex version
scala> def splitList(line: String, delim: List[String]): Unit = delim match {
| case head :: tail => for(item <- line.split(head).toList) splitList(item, tail)
| case Nil => println(line.trim)
| }
splitList: (line: String,delim: List[String])Unit
scala>
scala> def listline(line: String) = splitList(line, List(",","and","&"))
listline: (line: String)Unit
scala> listline("abc,def,ghi, jkl and mno & pqr")
abc
def
ghi
jkl
mno
pqr
Without prompt:
def splitList(line: String, delim: List[String]): Unit = delim match {
case head :: tail => for(item <- line.split(head).toList) splitList(item, tail)
case Nil => println(line.trim)
}
def listline(line: String) = splitList(line, List(",","and","&"))
// Simple
// def listline(line: String) = for(i <- line.split(",").toList) println(i.trim)
No comments:
Post a Comment