tony
tony

Reputation: 137

How to replace this sequence of characters

I have a long string and when I find the sequence of three characters consisting of 0x0d 0x0a 0x20 I will replace these three with 0x5c 0x6e 0x20. The problem is that I don't get a match after 0x0d 0x0a 0x20 in the first if clause below.

 if (allText.IndexOf(@"\r\n ") != -1)
 {
     allText = Regex.Replace(allText, @"\r\n ", @"\n ");                                     
     if ( allText.IndexOf(@"\n ") != -1)
     {
     }
 }

//Tony

Upvotes: 1

Views: 567

Answers (1)

Leon Barkan
Leon Barkan

Reputation: 2703

string is immutable so you need to assign it to new one or the same.

if i get you right you can change all the instances of 0x0d 0x0a 0x20 in your allTest string to 0x5c 0x6e 0x20 by using regular string Method Replace.

string textThatShouldBeReplaced = @"0x0d 0x0a 0x20";
string textToReplace = @"0x5c 0x6e 0x20";

if (allText.IndexOf(@"\r\n ") != -1)
   {
      if( allText.IndexOf(@"\n ") != -1)
        {
           allText = allText.Replace(textThatShouldBeReplaced,textToReplace);
        }
 }

Upvotes: 1

Related Questions