Home > Blockchain >  How to swap words in strings
How to swap words in strings

Time:02-11

I have a string in my code like:

val string = "
|3
|<sup>3</sup>{{P|Mummy Zombie|2}}<sup>5</sup> {{P|Mummy Zombie|2}}
|None
"

The sample code is shorter, will have more <sup></sup> {{zombie}} and the mummy zombie might be like conehead zombie, buckethead zombie... there is like a hundred types of zombies

how can i change it to like:

"
|3
|{{P|Mummy Zombie|2}}<sup>3</sup> {{P|Mummy Zombie|2}}<sup>5</sup>
|None
"

CodePudding user response:

If you don't mind regex:

val s = """
|3
|<sup>3</sup>{{P|Mummy Zombie|2}}<sup>5</sup> {{P|Mummy Zombie|2}}
|None
"""
val regex = "(<[^>] >[^>] >)\\s*(\\{\\{[^}] \\}\\})".toRegex()
println(s.replace(regex, "$2$1"))
  • Related