简体   繁体   English

如何检查两个字符串的长度相同?

[英]How to check if two string are of the same length?

I want to check if two string are of the same length. 我想检查两个字符串的长度是否相同。 I tried the following, but it doesn't work. 我尝试了以下操作,但不起作用。

string passnew = "1233";
string passcnfrm = "1234";

if((passnew.Length&&passcnfrm.Length)>6 ||(passnew.Length&&passcnfrm.Length)<15)
{ 
    // ...
}

Why does it not work? 为什么不起作用? What do I need to change? 我需要更改什么?

if(passnew.Length == passcnfrm.Length && 
   passnew.Length > 6 && passnew.Length < 15)
{
    // do stuff
}

You are missing some basic syntax lessons. 您缺少一些基本的语法课程。 What you write inside of these brackets are conditions. 您在这些括号内写的是条件。 We have unary operators (operating on one thing), binary operators (two) and one tertiary operator (forget about that one). 我们有一元运算符(对一件事进行运算),二元运算符(对二个运算符)和一个三级运算符(对那一个运算符忘了)。

You cannot construct something like your "boundary test" with those easily. 您不能轻易地用它们来构建诸如“边界测试”之类的东西。

A possible way: 可能的方法:

(passnew.Length > 6) && (passcnfrm.Length > 6)

But you aren't testing if the length is equal anyway, even if you could use a syntax like that. 但是即使您可以使用这样的语法,也不会测试长度是否相等。 You seem to want to compare if both are longer than 6 chars and shorter than 15 chars. 您似乎想比较两者是否均大于6个字符且小于15个字符。 One at 7 and one at 14 would satisfy both conditions.. 一个7点和一个14点将同时满足这两个条件。

if(passnew.Length == passcnfrm.Length && 
   (passnew.Length < 15 && passnew.Length > 6))
{ 
    // ...
}

Checks both are same length, and either one is more than 6 and less than 15 characters long. 两者的检查长度相同,并且其中一个长度大于6并且小于15个字符。

that would be: 那将是:

if(passcnfrm.Length.Equals(passnew.Length))
 {
   //do stuff
 }

A probably better way to do it is: 一个可能更好的方法是:

if (( passnew != null && passcnfrm != null )
    ( passnew == passcnfrm )
&&  ( passnew.Length > 6 && passnew.Length < 15 ))
{
    // do stuff
}

Hides the length check inside the equality check which you'll probably need, it isn't in your question but the variable names make it pretty clear you're doing a password change function there. 将长度检查隐藏在您可能需要的相等检查中,这不在您的问题中,但是变量名使您清楚地知道您正在此处执行密码更改功能。 I added the null check to make sure the length checks don't throw a NullReferenceException, not needed in the example because you assign both manually, but might save some trouble if you're going to convert this to a method later on. 我添加了空检查,以确保长度检查不会引发NullReferenceException,在示例中不需要,因为您手动分配了两者,但是如果以后再将其转换为方法,可能会节省一些麻烦。

You can use like this: if (s.Contains(s1[i])) 您可以这样使用:if(s.Contains(s1 [i]))

or: 要么:

boolean equalsIgnoreCase(String anotherString); boolean equalsIgnoreCase(String anotherString);

or use this method: 或使用此方法:

public static int occurrence(string [] a, string a2) { int occ = 0; 公共静态int出现(字符串[] a,字符串a2){int occ = 0; for (int i = 0; i < a.Length;i++ ) { if(a[i].Equals(a2)) { occ++; for(int i = 0; i <a.Length; i ++){if(a [i] .Equals(a2)){occ ++; } }

        }
        return occ;
    }

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

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