开发者

VB.NET REPLACE function

I am using replace function to replace a character in the file

sw.WriteLine(Regex.Replace(strLine, "\\", Chr(13)))

This code is working fine, but now I want 开发者_JAVA百科to replace two times and I want to use the replace function twice. Something like this, but it is not working . Can anyone tell me how to use Replace function multiple times?

sw.WriteLine(Regex.Replace(strLine, "\\", Chr(13)).Replace(strLine, Chr(13), ""))


Your second Replace is using the String.Replace extension, not the Regex.Replace method.

The Regex.Replace function returns a string, not a regex, which is why your second regex call isn't working. For multiple Regex.Replace calls, you would have to do each one individually or modify your replacement statement.

You could probably just use the String.Replace function for this:

sw.WriteLine(strLine.Replace("\\", Chr(13)).Replace(Chr(13), ""))


sw.WriteLine(Regex.Replace(Regex.Replace(strLine, "\\", Chr(13)), Chr(13), "")

Here it is more laid out, so you can see what's going on:

Dim firstIteration = Regex.Replace(strLine, "\\", Chr(13))

Dim secondIteration = Regex.Replace(firstIteration, Chr(13), "")

sw.WriteLine(secondIteration)


Replace a carriage return in a string may be the following ways: str_souce = str_source.Replace(vbCrLf, "") str_souce = str_source.Replace(chr(13) & chr(10), "") str_souce = str_source.Replace(environment.newline, "")

if none above works, try the following one. It can even works for a third party software str_souce = str_source.Replace(vbCr, "").Replace(vbLf, " ")

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜