C
C#2y ago
Sal

✅ What is better, in this case, Replace() or Substring()

In Unity it seems that text input comes with a 'Zero Width Space Character' at the end, and that messes with the following logic since a word with 5 characters has 6 characters and this ZWS character ain't accepted everywhere. Both of this solutions work fine to remove the character, but I am here asking if there is good reasons to use one or another? or even other way maybe even in unity settings to avoid that behavior.
string zeroWidthSpace = new(new char[] { (char)8203 });
name = name.Replace(zeroWidthSpace, "");
string zeroWidthSpace = new(new char[] { (char)8203 });
name = name.Replace(zeroWidthSpace, "");
or
name = name.Substring(0, name.Length-1);
name = name.Substring(0, name.Length-1);
69 Replies
MODiX
MODiX2y ago
Sal#0955
REPL Result: Success
string name = "hello";
name+=(char)8203;
name = name.Substring(0, name.Length-1);
string result = $"{name} has {name.Length} characters";
result
string name = "hello";
name+=(char)8203;
name = name.Substring(0, name.Length-1);
string result = $"{name} has {name.Length} characters";
result
Result: string
hello has 5 characters
hello has 5 characters
Compile: 601.693ms | Execution: 42.546ms | React with ❌ to remove this embed.
MODiX
MODiX2y ago
Sal#0955
REPL Result: Success
string name = "hello";
name+=(char)8203;
string zeroWidthSpace = new(new char[] { (char)8203 });
name = name.Replace(zeroWidthSpace, "");
string result = $"{name} has {name.Length} characters";
result
string name = "hello";
name+=(char)8203;
string zeroWidthSpace = new(new char[] { (char)8203 });
name = name.Replace(zeroWidthSpace, "");
string result = $"{name} has {name.Length} characters";
result
Result: string
hello has 5 characters
hello has 5 characters
Compile: 625.734ms | Execution: 42.323ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle2y ago
did you check if this character is removed by .Trim()? that's designed to remove leading and trailing whitespace
MODiX
MODiX2y ago
Sal#0955
REPL Result: Success
string name = "hello";
name+=(char)8203;
name = name.Trim();
string result = $"{name} has {name.Length} characters";
result
string name = "hello";
name+=(char)8203;
name = name.Trim();
string result = $"{name} has {name.Length} characters";
result
Result: string
hello​ has 6 characters
hello​ has 6 characters
Compile: 473.508ms | Execution: 40.504ms | React with ❌ to remove this embed.
Sal
SalOP2y ago
I didnt know for sure but IsNullOrWhiteSpace() didnt recognise the space when the field was "empty"
ero
ero2y ago
It's hard to believe strings behave any differently in Unity
Sal
SalOP2y ago
they dont, do they?
ero
ero2y ago
If they didn't, why would there be a zero width space? This seems like a very big bug, if true
MODiX
MODiX2y ago
Sal#0955
REPL Result: Success
string name = "";
name+=(char)8203;
string zeroWidthSpace = new(new char[] { (char)8203 });
string result = $"\"{name}\" has {name.Length} characters";
result
string name = "";
name+=(char)8203;
string zeroWidthSpace = new(new char[] { (char)8203 });
string result = $"\"{name}\" has {name.Length} characters";
result
Result: string
"​" has 1 characters
"​" has 1 characters
Compile: 578.966ms | Execution: 46.675ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle2y ago
sounds like more of a potential issue with whatever UI control is being used
Sal
SalOP2y ago
it behaves the same way here
ero
ero2y ago
Yeah but you're manually adding that to your string. Are you adding a zero width space to your input in unity?
Sal
SalOP2y ago
I would argue you are right but they might have their reasons..
ero
ero2y ago
I don't think they do, that's a massive flaw
Sal
SalOP2y ago
no I am just simulating here... well I was firstly trying to react if the input box had some value before doing any logic and nothing worked , value is null, value == "", string.IsEmptyOrWhiteSpace(value) until some guys here helped me our and we find the character by parsing the "empty" string to a char
ero
ero2y ago
What're you using as the UI control for the input? You should report this as a bug
Sal
SalOP2y ago
the default input box
ero
ero2y ago
Unity Forum
TextMesh Pro - [Solved] Getting 'ZERO WIDTH SPACE'
Getting 'ZERO WIDTH SPACE' character at the end of text from Input field [ATTACH] Is it ok !?
ero
ero2y ago
Does this have anything to do with it? Also i don't think TMPro is the "default"
Sal
SalOP2y ago
gonna see, but i have no other choice than TMPro
sibber
sibber2y ago
it is in the latest version they removed the old renderer i think either way you cant use it
ero
ero2y ago
I thought it was like an external library
sibber
sibber2y ago
they aquired it a while ago
Sal
SalOP2y ago
hey, so i tried using the child "Text" game object and it returns the same char
ero
ero2y ago
The post says not to use the child
Sal
SalOP2y ago
first i was using the (input).text, now i tried the Text.text
Sal
SalOP2y ago
i think those are the only options
ero
ero2y ago
I don't know. You're not really showing us much
Sal
SalOP2y ago
you dont need much more, what do you want more? this is the whole tree of the input field, and i am telling you when i get the input text on both gameobjects they both have the char attached to the end
[SerializeField] TextMeshProUGUI nameHolder;
public void SaveName()
{
//this is the gameObject with the input
string name = nameHolder.text;
//here i can see the invisible char
Debug.Log(name.Length);
//this was my code before
//if (name == zeroWidthSpace)
// now i am trying with your post link
//if (string.IsNullOrWhiteSpace(name))
if (string.IsNullOrEmpty(name))
{
name = "Empty";
}
}
[SerializeField] TextMeshProUGUI nameHolder;
public void SaveName()
{
//this is the gameObject with the input
string name = nameHolder.text;
//here i can see the invisible char
Debug.Log(name.Length);
//this was my code before
//if (name == zeroWidthSpace)
// now i am trying with your post link
//if (string.IsNullOrWhiteSpace(name))
if (string.IsNullOrEmpty(name))
{
name = "Empty";
}
}
nameHolder was SaveName (Input) at first now I tried Text I think this is absolutely all
ero
ero2y ago
Can you show what fields exist in the class?
Sal
SalOP2y ago
sorry why? there you go
ero
ero2y ago
I mean don't, and just use substring What do i care That doesn't look like a TMP_InputField to me
Sal
SalOP2y ago
lol if you don't care why you came try to help. I have no intentions to lie. my question was what would be best choice for handling the behavior now we are getting into strange territory
ero
ero2y ago
The best choice to handle the behavior is to not have the behavior happen For that, we need the entire context of what you're doing So we can tell what not to do
Sal
SalOP2y ago
i lay out the entire context.....
ero
ero2y ago
Mhm
Sal
SalOP2y ago
brand new
Sal
SalOP2y ago
maybe its changed since you used it..
ero
ero2y ago
I have never used Unity
Sal
SalOP2y ago
then I repeat myself but i am just fetching the value from the input field. so why did you complain hat it doesnt look like TMP?
ero
ero2y ago
the post mentions a TMP_InputField.text. you're clearly not getting the text of a TMP_InputField
Sal
SalOP2y ago
Smadge post is from 2018, we are in 2022
sibber
sibber2y ago
2018 is not that old
ero
ero2y ago
i highly doubt it changed that much, if at all
sibber
sibber2y ago
in unity's case
ero
ero2y ago
unity aren't really known to innovate
Sal
SalOP2y ago
and i could call my variable WTV then use WTV.text
ero
ero2y ago
calling it a different thing doesn't give it a different type
Sal
SalOP2y ago
enough so that they are using TMP as their ui solution now.. @Ero you dont use unity why are you fixing with that? the post dont even show the type just the name...
ero
ero2y ago
[SerializeField] TextMeshProUGUI nameHolder; not a TMP_InputField
Sal
SalOP2y ago
LOL its new... now you have 3 options...
Sal
SalOP2y ago
and so far i figured i have to use the UGUI
ero
ero2y ago
it's clearly like a base class there's not even any documentation on this tf
Sal
SalOP2y ago
but i can tell you that if i use the non UGUI i cant assign it in the inspector
ero
ero2y ago
idk man, i think i'd use something called an InputField for something like an input field
sibber
sibber2y ago
TMP_InputField is the input field
ero
ero2y ago
crazy
Sal
SalOP2y ago
yes it does exist such thing x) and i just tried it works without the char
ero
ero2y ago
🤯 maybe don't be like that the next time you ask people to spend time out of their day to help you i can also recommend google
ero
ero2y ago
ero
ero2y ago
this is literally what i searched to find that post
sibber
sibber2y ago
lol you dont even have to go to the page
Sal
SalOP2y ago
i think you are a bit of an arrogant, thanks for the time, but good will doesnt come with strings atached, this is certainly the best solution, but you dont use the software, and you expect that because a 4y old link says something i got to instantly understand that was the type itself that was wrong, i did a lot of learning (not enought, never enough) and sorry if i didnt realize it at first , and doubted your advice. but i tried to follow along. and you just slap like i didnt help the conversation.
sibber
sibber2y ago
he wasnt being arrogant you couldve tried his suggestion even if you didnt think it would work
Sal
SalOP2y ago
i didnt understand when i understand i did
sibber
sibber2y ago
then say you dont understand nothing wrong with that
ero
ero2y ago
programming is programming. you don't really need to understand the software to know that a link like that will definitely have valuable information googling well is just part of coding whatever problem you have, at least a dozen people have posted about
Sal
SalOP2y ago
thats all true, thanks guys
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server