简体   繁体   中英

Divide string into two parts based on separator in Scala

input:

myString = ""FILTER=(ID=123,Description=456)""

output:

FILTER, (ID=123,Description=456)

basically divide the string into two parts How can i achieve this ?

Want something equivalent to str.partition(sep) as in python

You want split with a limit parameter (but you don't get the separator as an element as in the Python partition )

val myString = "FILTER=(ID=123,Description=456)"     
myString.split("=", 2)  
//> res0: Array[String] = Array(FILTER, (ID=123,Description=456))

It's actually a java method - see here

The span -method could also be helpful for you

   val myString = "FILTER=(ID=123,Description=456)"
   //myString: String = FILTER=(ID=123,Description=456)

   myString.span(_!='=')
   //res9: (String, String) = (FILTER,=(ID=123,Description=456))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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