0

Using vb.net and visual studio 2012.

I have a bunch of strings using custom text replacement. By that I mean that they are all one-line strings using, for example, "&1" to replace 'vbcr' and so on. I have to take this string and replace all the "&1" by a vbcr.

I tried using regex and stringbuilder replace. Here is an example:

finaltext = firsttext.Replace("&1", vbcr)

But doing it this way results in replacing the "&1" by a simple space. I thought that vbcr was the problem but I tried to reverse my code by:

finaltext = firsttext.Replace(vbcr, "&1")

The vbcr were correctly replaced by "&1" so I don't understand why my original code is not working.

I know it's possible using a long complicated custom function but I would prefer to avoid this solution if possible.

4

1 回答 1

0

According to MSDN, the syntax of String.Replace states that the first argument is the oldValue and the second argument is the one that replaces it.

Also, if you need newlines, you should be using Environment.NewLine:

finaltext = firsttext.Replace("&1", Environment.NewLine)

Environment.NewLine is easier to read and it also takes care of the platform for you, being

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

于 2013-04-21T01:50:26.593 回答