c# - 如何从字符串中去掉反斜杠 ("\") 字符

标签 c# str-replace

有没有办法避免字符串中的“\”字符?

//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/

相关文章:

c# - 将 <text> 转换为字符串?

c# - WPF:PropertyChangedCallback 只触发一次

PHP,从数组读取时str_replace出现问题

javascript - 如何在脚本标签中使用 HTML 将字符串替换为 javascript

php - 语法高亮显示,语法不区分大小写

PHP Str 用特殊 html 字符替换函数

Ruby - 如果实例不匹配,则替换字符串中的单词

C# 如何检查属性是否返回 null?

c# - 用任务代替线程

c# - 安装 Windows 服务 - 未完成帐户名和安全 ID 之间的映射