Using:
string temp2 = temp.Replace("\n","\r\n")
for each match "\ n" in the temp line, the system creates a new line with a replacement.
This does not happen with StringBuilder because StringBuilder is changed, so you can actually change the same object without having to create another.
Example:
temp = "test1\ntest2\ntest3\n"
With the first method (string)
string temp2 = temp.Replace("\n","\r\n")
equivalently
string aux1 = "test1\r\ntest2\ntest3\n" string aux2 = "test1\r\ntest2\r\ntest3\n" string temp2 = "test1\r\ntest2\r\ntest3\r\n"
Using the Secon Method (StringBuilder)
string temp2 = new StringBuilder(temp).Replace("\n","\r\n").toString()
equivalently
Stringbuilder aux = "test1\ntest2\ntest3\n" aux = "test1\r\ntest2\ntest3\n" aux = "test1\r\ntest2\r\ntest3\n" aux = "test1\r\ntest2\r\ntest3\r\n" string temp2 = aux.toString()
Dani corretja
source share