-1

I have an array of names concatenated with an _ ex: string[] samples = ["Test_test","Test2_blah", "Test3_"].

At some point in my code I want to try and validate the value after the _ to check if it's empty or null, and if it is, remove it from the array like so :

string[] splitSample= samples[2].Split(new char[] { '_' }, 2);

if(!string.IsNullOrWhiteSpace(splitSample[1]))

The problem i'm running into is that splitSample[1] is "", when i check the length of the string it's 1, not 0 but in Visual Studio 2017 it shows empty quotes. Is there a way to actually see the value that's invisible or what's actually going on?

EDIT: Here's a pic of the immediate window when i check the array valueenter image description here

john
  • 3,949
  • 7
  • 34
  • 56

1 Answers1

3

Depending on how they get rendered, some Unicode characters can be invisible (i.e., "") when represented (e.g., "\u200C", "\u2063", and check this answer for more).

Now, your string has a length (>0), and you'd like to know what it actually represents. There are many ways to achieve this, one of them is to convert your string to hex. Here's an example using the Unicode character mentioned above:

static void Main(string[] args)
{
    string invisibleChar = "\u200C";
    string[] samples = { "Test_test", "Test2_blah", "Test3_" + invisibleChar };
    string[] splitSample = samples[2].Split(new char[] { '_' }, 2);

    // Prints "Test3_" (or "Test3_?" if you use Console.Write).
    Debug.Print(samples[2]);
    Debug.Print(splitSample.Length.ToString());     // 2

    if (!string.IsNullOrWhiteSpace(splitSample[1]))
    {
        Debug.Print(splitSample[1].Length.ToString());    // 1
        // Prints "" (or "?" in Console).
        Debug.Print(splitSample[1]);

        var hex = string.Join("", splitSample[1].Select(c => ((int)c).ToString("X2")));
        // Prints "200C"
        Debug.Print(hex);
    }

    Console.ReadLine();
}

Note that because you're using !string.IsNullOrWhiteSpace, you could be missing other Unicode characters (e.g., "\u00A0") because they're considered a white space. So, you should ask yourself whether you want to also check for these or not.

Hope that helps.

  • thanks for such an informative post. I checked the value after `Flickr_` and the hex ended up being `FE0E` – john Feb 12 '18 at 18:42
  • 1
    Well, glad I was able to help. You can also get more information about this type of character in this [Wikipedia article](https://en.wikipedia.org/wiki/Variation_Selectors_(Unicode_block)). – 41686d6564 stands w. Palestine Feb 12 '18 at 18:51