简体   繁体   English

如何在C#中检查两个字符串的长度是否相等?

[英]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. 我是c#和unity的新手,想知道如何编写此代码来检查两个字符串长度是否相等。

with this code unity system shows this error: error CS1061: Type char' does not contain a definition for Length' and no extension method Length' of type char' could be found. 具有此代码统一系统的代码将显示此错误:错误CS1061: char' does not contain a definition for类型char' does not contain a definition for Length' char' does not contain a definition for并且找不到char' Length' of type扩展方法Length' of type

for (int i = 0; i < Answers.Length; i++) 
{
    if (GetAnswer[i].Length == Answers[i].Length) 
    {
        //Do something
    }
}
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) 应该检查一个字符串的长度是否与另一个字符串相等。代码的问题是,字符串是char数组,因此它计算char的长度(不存在)

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 您还需要增加for的值以获取正确的长度,否则Answers.Length始终小于1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM