开发者

How to get rid of backslash("\")character from a string

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];
        开发者_JAVA技巧     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 ?


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

    byte[] bResult = ToByteArray(b1);
    strOutput  = Encoding.ASCII.GetString(bResult).Replace("\\\"","");

EDIT:

I believe in your case normally my answer would work, BUT

0 '\0'

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 (\)

CALL Replace("\0","0")


    string strOutput = "\0\0\0\0\0\0\0\0" 
    string strOutput = Encoding.ASCII.GetString(strOutput).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.


What about

StringBuilder sb = new StringBuilder();

foreach( byte b in bResult )
{

   sb.Append( String.Format("{0:00000000}\n", b ) ;
} 

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜