0

I have a string with the following value: var myString = "17124\0\0\0";

I need to do two things with the string:

  1. Get the string length
  2. Save the string to the database

Unfortunately, \0 is c# is the equivalent to null.

I have tried several things including replacing \ with \ myString.Replace(@"\", @"");

I have also tried getting the length by using the following code: var myLength = myString.Length;

Everything that I try treats every combination of \0 as null.

Important: I cannot strip \0 out of the string, because that is the value of the string.

Scott Hutchinson
  • 1,703
  • 12
  • 22
Internet Engineer
  • 2,514
  • 8
  • 41
  • 54
  • Does this answer your question? [How to Remove '\0' from a string in C#?](https://stackoverflow.com/questions/3387733/how-to-remove-0-from-a-string-in-c) – gunr2171 Dec 28 '20 at 18:07
  • _Unfortunately, \0 is c# is the equivalent to null._ No, it isn't. `\0` doesn't have any special meaning inside the C# libraries. There are libraries with external dependancies (Winforms for example) that don't handle it correctly because the external part is written in C and consider the first `\0` to be a string terminator, so that `"A\0B"` becomes `"A"` – xanatos Dec 28 '20 at 18:18
  • 1
    `var myString = @"17124\0\0\0";` – Mihal By Dec 28 '20 at 18:19
  • 1
    What is expected behavior? You can not store string with \0 in DB? Try converting it to bytes and save it that way. I don't belive that Lenght don't work with such string. Please clarify question. – Bogdan Mart Dec 28 '20 at 18:46
  • Having such a string is not problematic in C# itself, the `myString.Length` property will return `8` in this case. Please [edit] your question to include a detailed description of the actual problem you have. Also include where you have problems in getting the string length and what the problem is saying this value in a database. If possible, provide a [mcve] which can be compiled and tested by others. – Progman Dec 28 '20 at 21:01

2 Answers2

3

I expect you want to have a string containing "\0" as text, just not interpreted as null character, so you can simply escape the escape character '\', like so:

var myString = "17124\\0\\0\\0";

Or you can use a so-called verbatim string literal, like so:

var myString = @"17124\0\0\0";

In verbatim string literals, the escape character is ignored and you can keep the usual (more human readable) text representation.

In any of the above cases, the string object should work like expected, e.g. returning the expected value as the length of the string.

If you want to get rid of the '\' in your literal, you can then call the Replace function, like you already tried, using normal or verbatim string literals.

myString = myString.Replace("\\", "");
myString = myString.Replace(@"\", @"");

But, if you want to remove the null character from your string completely, just replace the null charaters ("\0"), instead of the backslash ("\") in your string by an empty literal ("") or the character 0 ("0").

myString = myString.Replace("\0", "");
myString = myString.Replace("\0", "0");
Cellcon
  • 1,245
  • 2
  • 11
  • 27
1

You could strip out the null characters like this:

        static void Main(string[] args) {
            string myString = "17124\0\0\0";
            string myStringWithoutNulls = myString.TrimEnd('\0');
            var len = myStringWithoutNulls.Length;
            WriteLine($"Length is: {len}");
            WriteLine($"String without nulls is: {myStringWithoutNulls}");
        }
Scott Hutchinson
  • 1,703
  • 12
  • 22