C
C#2y 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
mtreit2y ago
Print out the length - is it definitely zero? Because no, that code should work if the string is actually empty.
Sal
SalOP2y ago
Sal
SalOP2y ago
Sal
SalOP2y ago
i can try that
mtreit
mtreit2y ago
I don't always trust the debugger...
Sal
SalOP2y ago
Sal
SalOP2y ago
it has 1 "length" thats odd because i also print the "name" and shows empty (nothing apears)
mtreit
mtreit2y ago
Try IsNullOrWhitespace Might be something like a newline character? Or convert it to a char[] and see what the char value is.
Sal
SalOP2y ago
didnt work
mtreit
mtreit2y ago
What's the char value?
Sal
SalOP2y ago
i will see now
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
The suspense is killing me
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Sal
SalOP2y ago
Sal
SalOP2y ago
x)
mtreit
mtreit2y ago
Need the numeric value
Sal
SalOP2y ago
this means its a "? weird to me
mtreit
mtreit2y ago
(int)name[0]
(int)name[0]
Get that So we can figure out what character it actually is
Sal
SalOP2y ago
8203
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
zero width space
Sal
SalOP2y ago
ZERO WIDTH SPACE ups yeps i never knew it existed
mtreit
mtreit2y ago
I'm surprised IsNullOrWhitespace doesn't handle that.
333fred
333fred2y ago
ZWSP is not part of the space category iirc
Sal
SalOP2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
Sal
SalOP2y ago
thanks guys, great help
MODiX
MODiX2y 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
mtreit2y ago
IT HAS SPACE IN THE NAME! 😛
333fred
333fred2y ago
Blame the unicode consortium
Sal
SalOP2y ago
btw why would we want a non character?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
mtreit
mtreit2y ago
That is a tricky one - the debugger showing it as an empty string really makes it kind of non-obvious.
Sal
SalOP2y ago
because thats what it "looks" like
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
333fred
333fred2y 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 User2y 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