1

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

3 Answers 3

4

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), ""))
Sign up to request clarification or add additional context in comments.

Comments

1
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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.