Is there any easy way how to detect (during debugging), that string contains some hidden character (for example zero width space)?
Example: During debugging I'm comparing two differnet strings and they seem equal to my eyes. Of course they differ in some hidden charaters. How to find the difference?
I used string.ToCharArray() method in "Immediate window" of Visual Studio but there must be more comfortable way.
The zero width space is Unicode character U+200B. (HTML ​). It's remarkably hard to type. On Windows you can type Alt-8203.
The zero-width space (), abbreviated ZWSP, is a non-printing character used in computerized typesetting to indicate word boundaries to text-processing systems in scripts that do not use explicit spacing, or after characters (such as the slash) that are not followed by a visible space but after which there may ...
To remove zero-width space characters from a JavaScript string, we can use the JavaScript string replace method that matches all zero-width characters and replace them with empty strings. Zero-width characters in Unicode includes: U+200B zero width space.
The code ​ is the HTML code for the zero width space.
You can use this in the immediate window:
str.Contains("\u8203");
Or put it in the watch window so you'll just have to click the refresh button near the watched value to see the result, rather than re-entering it to the immediate (although you can always press up and then enter to re-enter the last command!)
To check for ANY hidden character, you can either have a static array with all hidden characters and check:
HIDDENS.Any(c => str.Contains(c.ToString())
And preferable even save the hidden characters as one-length strings and then do:
HIDDENS.Any(str.Contains)
OR you could be really sophisticated and do THIS:
private static readonly Bitmap BMP = new Bitmap(1000, 1000);
private static readonly Graphics GRAPHICS = Graphics.FromImage(BMP);
private static readonly Font FONT = new Font("Arial", 20);
private static readonly RectangleF RECT = new RectangleF(0, 0, 1000, 1000);
public static bool CheckInvisibleChars(string text)
{
var stringFormat1 = new StringFormat(StringFormatFlags.MeasureTrailingSpaces);
stringFormat1.SetMeasurableCharacterRanges(
Enumerable.Range(0, text.Length - 2).Select(i => new CharacterRange(i, 1)).ToArray());
return GRAPHICS.MeasureCharacterRanges(text, FONT, RECT, stringFormat1).Any(
reg => reg.GetBounds(GRAPHICS).Width.Equals(0f));
}
From here it should also be easy to return information about each hidden character, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With