2016-08-17 69 views
0

我有一個很長的字符串,當我找到由0x0d 0x0a 0x20組成的三個字符的序列時,我將用0x5c 0x6e 0x20替換這三個字符。 問題是我在0x0d 0x0a 0x20之後的第一個if子句中沒有找到匹配項。所以你需要將其分配到新的或相同如何替換這個字符序列

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

//託尼

+1

什麼是allText? –

+0

這只是很多文字 – tony

+1

它是否包含@「\ r \ n」...如果是的話,它應該通過第一個條件 –

回答

0

字符串是不可改變的。

如果我能幫到您,您可以使用常規字符串Method Replace將您所有測試字符串中的所有0x0d 0x0a 0x20實例更改爲0x5c 0x6e 0x20

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); 
     } 
}