#Comparing 2 strings

1 messages · Page 1 of 1 (latest)

neat bobcat
#

i am trying to get the string input from an input field after its sent then compare it to a string from an csv excel file. i am debugging and they have the same values but when i run the code it doesnt work. pls help

#

i know that the boolean is inverted

violet wren
#

have you tried .Equals()?

neat bobcat
#

like ras.Equals(rs)?

violet wren
#

ye

neat bobcat
#

i will try rn

#

doesnt work

#

im copying the same value but still doesnt work'

violet wren
#

how about with tostring?

neat bobcat
#

alr tried and it didnt work

violet wren
#

ras.ToString().Equals(rs.ToString())

#

ah

neat bobcat
#

oh ok

#

still no

#

idk what am i doing wrong but its so frustrating

violet wren
#

damn yeah im not sure

neat bobcat
#

how would u compare an input text with a string

#

maybe my method is worng

#

maybe the way i am taking the input

violet wren
#

I would imagine .equals would work like a charm xD

#

perhaps. debug the .text part

neat bobcat
#

how

#

like debug ras

violet wren
#

yeah see what rasp.text gives

neat bobcat
#

k

#

debug is not a method

#

idk why

mild stag
#

because it's not

#

¯_(ツ)_/¯

#

It's a class

violet wren
#

Debug.Log()

neat bobcat
#

:)))

#

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

mild stag
#

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)

neat bobcat
#

bouth without spaces

mild stag
#

and still shown as not equal?

neat bobcat
#

yep

mild stag
#

🤔

#

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

neat bobcat
#

ok

#

so ima debug for bouth strings

mild stag
#

mhm

#

You'll need to import System.Linq if you aren't already, for the Select call

neat bobcat
#

ok

#

still 2 errors

mild stag
#

you need to import System.Linq

#

and System actually

neat bobcat
#

imported both but the select still isnt ok

mild stag
#

oh

#

rasp is not a string

#

that's why

neat bobcat
#

ye i figured out rn too

#

i typed rasp.text

mild stag
#

👍

#

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

neat bobcat
mild stag
#

there ya go

#

0B is the ASCII code for a vertical tab

#

so there was a crafty hidden whitespace character there

neat bobcat
#

how do i delete it?

mild stag
#

go to the text box, hit Ctrl+A to select all of it, and hit Delete. and then just enter 2

neat bobcat
#

ok

#

sis it

#

did it

mild stag
#

working now?

neat bobcat
#

but whenever i enter sum it puts the whitespace back

mild stag
#

🤨

neat bobcat
#

yes

#

i am sad rn

mild stag
#

well, there is a workaround

#

but it's incredibly hacky and ugly

#

it'll work though

neat bobcat
#

if it works

#

but i wonder why does it do this

mild stag
#

I wonder too

neat bobcat
#

should i just delte the input and put it back?

mild stag
#

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

neat bobcat
#

whitespace doesnt mean a normal space, right?

mild stag
#

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

neat bobcat
#

F

#

even if its like :

#

2 2

#

?

mild stag
#

well that's just a space

neat bobcat
#

so it gets erased?

mild stag
#

only from the start and end

#

it won't remove spaces in the middle

neat bobcat
#

ohho

#

nice

mild stag
#

so "this string " will turn into "this string"

neat bobcat
#

this is amazing

#

ur amazing

#

how did u learn this

mild stag
#

I've used C# for a very long time LUL

neat bobcat
#

thank you so much

mild stag
#

you're welcome

neat bobcat
#

i havent even tested it but i am so thankful

mild stag
#

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

neat bobcat
#

thank you for trying

#

hey

#

1 more thing

#

this is the code now

mild stag
#

hm?

neat bobcat
#

still doesnt work

mild stag
#

trim the rs too

#

rs.ToString().Trim()

#

oh wait

#

you already did

neat bobcat
#

yes

#

i did

#

yes ;-;

mild stag
#

oh is rs a string?

#

you don't need ToString if it's already a string

neat bobcat
#

ok

#

yes so its still a whitespace there

mild stag
#

what are you logging

neat bobcat
#
Debug.Log(BitConverter.ToString(THE_STRING.Select(c => (byte)c).ToArray()));```
#

the thing u sent me

mild stag
#

are you logging the original text, or the trimmed text?

#

because the trimmed text should match

neat bobcat
#

the trimmed

mild stag
#
Debug.Log(BitConverter.ToString(ras.Select(c => (byte)c).ToArray()));
Debug.Log(BitConverter.ToString(rs.Select(c => (byte)c).ToArray()));

(after trimming both)

neat bobcat
mild stag
#

the 0B still gets dumped in there wtf??

#

That... should not happen

neat bobcat
#

should i just delete the input and make another one?

neat bobcat
#

xD i just noticed there are 145 msges under this question

#

stupid input system...

mild stag
#

well this is certainly an interesting problem after all

#

that vertical tab has no right being here var

#

I have no idea why it's there but I'm gonna go ahead and blame Unity

mild stag
neat bobcat
#

ok it might take a while but i will msg under the thread the results

mild stag
#

cool 👍

neat bobcat
#

👍

#

is it ok if i delete the placeholder?

mild stag
#

there's no harm in it if you don't need it

neat bobcat
#

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?

mild stag
#

yes but you shouldn't need to do worry, if you get a reference to the root text object you can just do .text

neat bobcat
#

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

mild stag
#

what on earth

neat bobcat
#

how do i take the text from the input

severe raven
# neat bobcat 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

Unity Learn

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.

neat bobcat
#

I will

#

Thanks for answering

severe raven
# neat bobcat I will

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.
leaden marlin
violet wren
#

was just throwing ideas 😂

leaden marlin
#

but like the == operator calls .Equals

#

so there's no reason they would work any different

mild stag
#

I have absolutely no idea why you're giving Java examples in #1006674323458764851

severe raven
mild stag
#

You can also use a StringComparison value for Compare

#

In fact there's quite a few overloads

severe raven
leaden marlin
#

personal experience is not evidence

mild stag
soft dune
#

NB: a.Equals(b) is the only one from the lot that will throw a NullReferenceException if a is null.

neat bobcat
#

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

mild stag
#

I said to do that

mild stag