#Comparing 2 strings
1 messages · Page 1 of 1 (latest)
have you tried .Equals()?
like ras.Equals(rs)?
ye
how about with tostring?
damn yeah im not sure
how would u compare an input text with a string
maybe my method is worng
maybe the way i am taking the input
yeah see what rasp.text gives
Debug.Log()
:)))
i didnt use unity in such long time
kinda ashamed
k ima run and lets see
returns 2
and i entered 2
and the other string is 2
make sure it's exactly the same. a string with spaces would not be considered equal. i.e. "2" is not equal to "2 " (and when it's in something like a text box, that kind of thing is easy to miss)
and still shown as not equal?
yep
🤔
okay try this
Debug.Log(BitConverter.ToString(THE_STRING.Select(c => (byte)c).ToArray()));
Replace THE_STRING with the name of the string. This will output the bytes of the string, in numeric form
maybe there's a hidden whitespace char or something
imported both but the select still isnt ok
👍
Run it, and it should print out the bytes in hex form
and you can then compare that with the bytes of the other string
there ya go
0B is the ASCII code for a vertical tab
so there was a crafty hidden whitespace character there
how do i delete it?
go to the text box, hit Ctrl+A to select all of it, and hit Delete. and then just enter 2
working now?
but whenever i enter sum it puts the whitespace back
🤨
I wonder too
should i just delte the input and put it back?
but for hacky workaround: you could just Trim the strings. foo.Trim() will return foo but with all whitespace removed from the start and end of the string
but a better solution is finding out why vertical tab gets put there in the first place
whitespace doesnt mean a normal space, right?
whitespace includes anything that isn't a visible character. so space, tab, new line, stuff like that
anything that's considered a "space" in some form
well that's just a space
so it gets erased?
so "this string " will turn into "this string"
I've used C# for a very long time 
thank you so much
you're welcome
i havent even tested it but i am so thankful
I'd still try and find the cause for the space being there in the first place, but Trimming it will do the job even if it's very hacky
hm?
what are you logging
Debug.Log(BitConverter.ToString(THE_STRING.Select(c => (byte)c).ToArray()));```
the thing u sent me
are you logging the original text, or the trimmed text?
because the trimmed text should match
the trimmed
Debug.Log(BitConverter.ToString(ras.Select(c => (byte)c).ToArray()));
Debug.Log(BitConverter.ToString(rs.Select(c => (byte)c).ToArray()));
(after trimming both)
should i just delete the input and make another one?
well this is certainly an interesting problem after all
that vertical tab has no right being here 
I have no idea why it's there but I'm gonna go ahead and blame Unity
I mean it's worth a shot, at this point
ok it might take a while but i will msg under the thread the results
cool 👍
there's no harm in it if you don't need it
ok so
in an input field
there is the input field as a parent
then a text placeholder and a text
i am getting the input from the text one, no?
yes but you shouldn't need to do worry, if you get a reference to the root text object you can just do .text
well then i got the same problem
ok so i found sum interesting
the problem stays if i am looking at the text under the input field
but i read the input field then there is no whitespace
that blue little line form the second picture is the whitespcae
space
what on earth
how do i take the text from the input
https://learn.unity.com/tutorial/working-with-textmesh-pro#
Check it out. All your answers in one place
TextMesh Pro is an easy-to-use system for high-quality text. It has many text appearance and formatting options, and is an easy way to add a professional touch to any project’s user interface. In this tutorial, you will learn to prepare fonts for use in TextMesh Pro, create new TextMesh Pro objects, and alter those objects.
and as per your post title you can compare two strings in various was
let s1 and s2 be two strings to be compared
string.Equals(s1, s2)//returns boolean
string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase)//returns boolean
string.Compare(s1,s2)//returns integer compares them lexicographically
string.Compare(s1,s2, true)//returns integer compares them lexicographically```
Or you may create your own methods for comparing them. a protype would be to compare them lexicographically using for loop by extracting each character.
why would that work any different?
was just throwing ideas 😂
but like the == operator calls .Equals
so there's no reason they would work any different
This isn't Java
I have absolutely no idea why you're giving Java examples in #1006674323458764851
Shit!! Sorry!! I am changing it.
You can also use a StringComparison value for Compare
In fact there's quite a few overloads
https://learn.microsoft.com/en-us/dotnet/api/system.string.compare
https://learn.microsoft.com/en-us/dotnet/api/system.string.equals
== fails drastically for string. works for char and int. Don't ask for the explanation. IDK, but it is a practical experience of mine....... 🙂
personal experience is not evidence
it doesn't fail at all. it literally calls .Equals. a == b is identical to string.Equals(a, b) or a.Equals(b)
NB: a.Equals(b) is the only one from the lot that will throw a NullReferenceException if a is null.
ok so
what am i suposed to try?
for anyone wondering how to solve this i did it
just takee the text from the imput field and not from the text child of the input field
thanks to everyone who helped me
I said to do that
get a reference to the root text object