如何从字符串中删除反斜杠(“\”)字符
有没有一种方法可以避免字符串中的“\”字符?
//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 忽略 '\' 字符
但是还有其他更好的方法来处理这种情况吗?
Is there a way, where i can avoid the '\' character from a string ?
//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);
Output
The string strOutput is = "\0\0\0\0\0\0\0\0"
But the desired output is 00000000
where ToByteArray could be a simple method as this
public static byte[] ToByteArray(BitArray bits)
{
byte[] ret = new byte[bits.Length / 8];
bits.CopyTo(ret, 0);
return ret;
}
Alternative 1 : i can ignore the '\' character using regular expressions or string.replace
But is there any other better way to handle such scenarios ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能只操作二进制行内的值。相反,正如您给出的替代方案,使用正则表达式或字符串替换函数来获取指定的结果集。
例如:
通过对您的代码调用额外的方法
编辑:
我相信在您的情况下我的答案通常会起作用,
BUT
也是 ASCII 字符集(仅在 0 中),这意味着您将获得相同的字符串 \0\0\0\0 .. 替换为反斜杠字符(\)
You cant just manipulate values inside a binary row.Instead,as you've given alternatives,use regular expressions or string replace function to get your designated result set.
For example:
By calling an extra method to your code
EDIT:
I believe in your case normally my answer would work,
BUT
is also in ASCII character set(only in 0),that means you're given the same string \0\0\0\0 .. replacing with backslash character (\)
如果您的问题是“是否有比使用 string.replace 更好的方法来删除反斜杠,我会说答案通常是否定的,但这取决于您想要做什么。
您是否在寻找原始速度?您是否认为编译正则表达式并应用它的开销太慢?正则表达式引擎通常经过很好的优化,如果您经常使用它们,您可以查看各种“替换”函数的源代码。图书馆以了解专业人士的工作方式 。
但我认为你的问题是你是否可以编写自己的字符串处理函数,该函数比正则表达式替换更快,因为你知道你可以分配最大大小的字符串缓冲区,遍历输入字符串并将每个字符复制到输出中 除了反斜杠。当然,这是一个简单的循环,也许编译器会将其优化为比正则表达式替换更快的东西,但谁知道呢?通常这些程序员引发的优化不值得麻烦(代码)。较长且容易出错)。至少,如果这就是您所追求的,请进行一些分析。
If your question is "is there a better way to remove backslashes than to use
string.replace
I would say the answer is generally no, but it depends on what you are looking to do.Are you looking for raw speed? Do you think the overhead of compiling a regex and applying it is too slow? Regex engines are generally fairly well optimized and if you use them often give good performance. You can look at the source code of "replace" functions in various libraries to get an idea of how pros do things.
But I think your question is whether you can write your own string processing function that is faster than a regex replace because you know you can allocate a string buffer of the max size, walk through the input string and copy into the output every character except a backslash. Sure that is a simple loop and maybe the compiler will optimize it to something faster than a regex replace, but who knows? Generally these programmer-induced optimizations are not worth the trouble (the code is longer and error-prone). At least, do some profiling if this is what you are after.
怎么样?
strOutput = sb.ToString();
我知道,有些不同,但我相信这更符合您的需求!因为在某些时候你必须从二进制转换为字符!如果不是,您将不知道它是二进制零值还是 char 0 的 ascii 表示!
马里奥
What about
strOutput = sb.ToString();
I know, something different but I do believe this is more what you want! Since at some point you have to convert from binary to char! If not, you will not know if it is a binary zero value or the ascii represenation of the char 0!
hth
Mario