tags:

views:

55

answers:

1

I have the following string:

val s1:String = "1. my line 1\n2. my other line\n3. my 3rd line\n4. and so on"

Now, I want transform at other:

val s2:String = "<b>1. </b>my line 1\n<b>2. </b>my other line\n<b>3. </b>my 3rd line\n<b>4. </b>and so on"

What is better way to do it?

+3  A: 
s1.replaceAll("""(?m)^(\d+\. )""", "<b>$1</b>")

Read: Find all occurences of "beginning of the line followed by one or more digits followed by a dot followed by a space" and replace them with the matched substring surrounded by <b> and </b>.

The (?m) bit makes it so that ^ means "beginning of line" instead of "beginning of string". The """ are so that there's no need to double escape.

sepp2k
Perfect, thanks so much!
isola009
And, also enclosing lines with <i> and </i>. Example:`"<b>1. </b><i>my line 1</i>\n<b>2. </b><i>my other line</i>\n<b>3. </b><i>my 3rd line</i>\n<b>4. </b><i>and so on</i>"`
isola009