Prateek
Prateek

Reputation: 241

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 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), ""))

Upvotes: 1

Views: 3304

Answers (3)

SIMP_Group
SIMP_Group

Reputation: 1

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, " ")

Upvotes: 0

LarsTech
LarsTech

Reputation: 81610

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), ""))

Upvotes: 4

user122211
user122211

Reputation: 493

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)

Upvotes: 1

Related Questions