masih namdar
masih namdar

Reputation: 25

How to check if two strings length are equal in c#?

I'm new in c# and unity and Wondering how could I write this code to check if two string lengths are equal.

with this code unity system shows this error: error CS1061: Type char' does not contain a definition forLength' and no extension method Length' of typechar' could be found.

for (int i = 0; i < Answers.Length; i++) 
{
    if (GetAnswer[i].Length == Answers[i].Length) 
    {
        //Do something
    }
}

Upvotes: 0

Views: 953

Answers (1)

ABovet
ABovet

Reputation: 11

if (yourString.Length == yourOtherString.Length)
{
 //dosomething
}

should check if a string is equals in length to another The problem with your code is that a string is an array of char so it calculate the length of the char (Which do not exist)

for (int i = 0; i < Answers.Length+1; i++) 
{
    if (GetAnswer.Length == Answers.Length) 
    {
    //Do something
    }
}

You also need to increment the value of the for to get the correct length otherwise the Answers.Length will always be short of 1

Upvotes: 1

Related Questions