C
C#3y ago
Sal

❔ empty string not being recognized

string name = nameHolder.text;
if (String.IsNullOrEmpty(name))
{
name = "New Save";
}
string name = nameHolder.text;
if (String.IsNullOrEmpty(name))
{
name = "New Save";
}
I started with if(name == "") tried string.Empty but when give it an empty name, and in debug I can see its an empty string, the statement is always false. I am using unity, am I missing something obvious?
39 Replies
mtreit
mtreit3y ago
Print out the length - is it definitely zero? Because no, that code should work if the string is actually empty.
Sal
SalOP3y ago
Sal
SalOP3y ago
Sal
SalOP3y ago
i can try that
mtreit
mtreit3y ago
I don't always trust the debugger...
Sal
SalOP3y ago
Sal
SalOP3y ago
it has 1 "length" thats odd because i also print the "name" and shows empty (nothing apears)
mtreit
mtreit3y ago
Try IsNullOrWhitespace Might be something like a newline character? Or convert it to a char[] and see what the char value is.
Sal
SalOP3y ago
didnt work
mtreit
mtreit3y ago
What's the char value?
Sal
SalOP3y ago
i will see now
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
The suspense is killing me
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Sal
SalOP3y ago
Sal
SalOP3y ago
x)
mtreit
mtreit3y ago
Need the numeric value
Sal
SalOP3y ago
this means its a "? weird to me
mtreit
mtreit3y ago
(int)name[0]
(int)name[0]
Get that So we can figure out what character it actually is
Sal
SalOP3y ago
8203
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
zero width space
Sal
SalOP3y ago
ZERO WIDTH SPACE ups yeps i never knew it existed
mtreit
mtreit3y ago
I'm surprised IsNullOrWhitespace doesn't handle that.
333fred
333fred3y ago
ZWSP is not part of the space category iirc
Sal
SalOP3y ago
at least the mystery is solved.. dont understand why this would be a default for empty input on anything ! i was completely lost
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Sal
SalOP3y ago
thanks guys, great help
MODiX
MODiX3y ago
mtreit#6470
REPL Result: Success
using System;
char c = (char)8203;
var s = new string(new char[] { c });
Console.WriteLine(string.IsNullOrWhiteSpace(s));
using System;
char c = (char)8203;
var s = new string(new char[] { c });
Console.WriteLine(string.IsNullOrWhiteSpace(s));
Console Output
False
False
Compile: 561.203ms | Execution: 63.676ms | React with ❌ to remove this embed.
mtreit
mtreit3y ago
IT HAS SPACE IN THE NAME! 😛
333fred
333fred3y ago
Blame the unicode consortium
Sal
SalOP3y ago
btw why would we want a non character?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit3y ago
That is a tricky one - the debugger showing it as an empty string really makes it kind of non-obvious.
Sal
SalOP3y ago
because thats what it "looks" like
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
333fred
333fred3y ago
​ U+200B ZERO WIDTH SPACE - Unicode Explorer
​ U+200B ZERO WIDTH SPACE, copy and paste, unicode character symbol info, commonly abbreviated ZWSP, this character is intended for invisible word separation and for line break control; it has no width, but its presence between two characters does not prevent increased letter spacing in justification
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server