C# 刪除指定字符串


想要刪除字符串中指定的數據,可以采用下面的方法,親測有效;(會把數據中單個 "C" 誤刪

 1             using System.Text.RegularExpressions;
 2             byte[] output = new byte[] { 0x75, 0x64, 0x61, 0x30, 0x01, 0x01, 0x01, 0x75, 0x64, 0x61, 0x31, 0x01, 0x01, 0x01, 0x75, 0x64, 0x61,0x32, 0x01, 0x01, 0x01, 0x75, 0x64, 0x61, 0x33, 0x01, 0x01, 0x01, 0x75, 0x64, 0x61, 0x34, 0x01, 0x01, 0x01, 0x75, 0x64, 0x61, 0x35, 0x01, 0x01, 0x01,0xCC,0xCC};
 3 
 4             for(int j = 0; j < output.Length; j++)
 5             {
 6                 output_str = output_str + output[j].ToString("X2") + " ";
 7             }
 8 
 9             Regex regex = new Regex(@"[CC]");
10             string response_fix = regex.Replace(response, "");
11     

如上圖代碼所示,將byte數組中的 CC 替換成 "",數據保存在txt文檔中如下圖所示;

 

如果數據中包含單個"C"字母需要保留,可以使用Replace直接進行替換,Replace方法有2個方法重載實現,一個是String Replace(String oldValue, String newValue),另一個是Replace(char oldChar, char newChar);前面的重載形式為以子字符串的形式來進行替換,而后面的重載形式為按照單個字符匹配進行替換。

 

string fix_string = paramstr.Replace("CC", "");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM