想要删除字符串中指定的数据,可以采用下面的方法,亲测有效;(会把数据中单个 "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", "");