Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a substring between two specific characters in Scala

I have this List in Scala:

List[String] = List([[aaa|bbb]], [[ccc|ddd]], [[ooo|sss]])

And I want to obtain the same List with the substrings between | and ] removed and | removed too.

So the result would be:

List[String] = List([[aaa]], [[ccc]], [[ooo]])

I tried something making a String with the List and using replaceAll, but I want to conserve the List.

Thanks.

like image 944
KonaKona Avatar asked Jan 05 '23 00:01

KonaKona


1 Answers

Here is a simple solution that should be quite good in performance:

val list = List("[[aaa|bbb]]", "[[ccc|ddd]]", "[[ooo|sss]]")
list.map(str => str.takeWhile(_ != '|') + "]]" )

It assumes that the format of the strings is:

  • Two left square brackets [ at the beginning,
  • then the word we want to extract,
  • and then a pipe |.
like image 162
Mikel San Vicente Avatar answered Jan 07 '23 12:01

Mikel San Vicente