어때요?
$ scala
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
def stripBrackets(str: String) = str.replaceAll("\\[", "").replaceAll("\\]", "")
def stripBrackets(ls: List[String]): List[String] =
ls match {
case Nil => ls
case List(head) => List(stripBrackets(head))
case List(head, last) => List(stripBrackets(head), stripBrackets(last))
case _ => stripBrackets(ls.head) :: ls.take(ls.size - 1).drop(1) ::: (stripBrackets(ls.last) :: Nil)
}
val singleElemList = List("[value1]")
val twoElemList = List("[value1", "value2]")
val multiElemList = List("[value1", "value2", "value3", "value4]")
// Exiting paste mode, now interpreting.
stripBrackets: (str: String)String <and> (ls: List[String])List[String]
stripBrackets: (str: String)String <and> (ls: List[String])List[String]
singleElemList: List[String] = List([value1])
twoElemList: List[String] = List([value1, value2])
multiElemList: List[String] = List([value1, value2, value3, value4])
scala> stripBrackets(singleElemList)
res0: List[String] = List(value1)
scala> stripBrackets(twoElemList)
res1: List[String] = List(value1, value2)
scala> stripBrackets(multiElemList)
res2: List[String] = List(value1, value2, value3, value4)
이 결과'목록 ("[값 1의 값 2가"]) '또는 'List ("[value1", "value2]")? – flavian
죄송합니다. 목록은 다음과 같습니다 : List ("[value1", "value2]") –
'value' 그 자체에 브래킷이 포함될 수 있습니까? 그렇지 않다면 가장 간단한 해결책은 괄호를 빈 문자열로 바꾸는 것입니다. –