想要刪除字符串中指定的數據,可以采用下面的方法,親測有效;(會把數據中單個 "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", "");