有没有办法避免字符串中的“\”字符?
//bit array
BitArray b1 = new BitArray(Encoding.ASCII.GetBytes("10101010"));
BitArray b2 = new BitArray(Encoding.ASCII.GetBytes("10101010"));
//Say i perform XOR operation on this
b1 = b1.Xor(b2);
//After the XOR oper the b1 var holds the result
//but i want the result to be displayed as 00000000
//So i convert the bitarray to a byte[] and then to string
byte[] bResult = ToByteArray(b1);
strOutput = Encoding.ASCII.GetString(bResult);
输出
The string strOutput is = "\0\0\0\0\0\0\0\0"
But the desired output is 00000000
其中 ToByteArray 可能是一个像这样的简单方法
public static byte[] ToByteArray(BitArray bits)
{
byte[] ret = new byte[bits.Length / 8];
bits.CopyTo(ret, 0);
return ret;
}
备选方案 1:我可以使用正则表达式或 string.replace 忽略 '\' 字符
但是还有其他更好的方法来处理这种情况吗?
最佳答案
您不能只操作二进制行内的值。相反,当您给出替代方案时,使用正则表达式或字符串替换函数来获取指定的结果集。
例如: 通过对您的代码调用一个额外的方法
byte[] bResult = ToByteArray(b1);
strOutput = Encoding.ASCII.GetString(bResult).Replace("\\\"","");
编辑:
我相信在您的情况下,我的回答通常会奏效, 但是
0 '\0'
也是 ASCII 字符集(仅在 0 中),这意味着你得到相同的字符串 \0\0\0\0 .. 替换为反斜杠字符 (\)
CALL Replace("\0","0")
关于c# - 如何从字符串中去掉反斜杠 ("\") 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907674/