C#中字符串的逻辑运算符

以下是可以在C#中的字符串上使用的逻辑运算符

运算符描述
&&称为逻辑AND运算符。如果两个操作数都不为零,则条件为true。(A && B)是错误的。
||称为逻辑或运算符。如果两个操作数中的任何一个都不为零,则条件为真。(A || B)为真。
称为逻辑非运算符。用于反转其操作数的逻辑状态。如果条件为真,则逻辑非运算符将为假。!(A && B)是正确的。

让我们看一个示例,该示例显示如何在字符串上使用逻辑AND运算符-

示例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

public class Demo {

   public bool CheckUnique(string str) {

      string one = "";

      string two = "";

      for (int i = 0; i < str.Length; i++) {

         one = str.Substring(i, 1);

         for (int j = 0; j < str.Length; j++) {

            two = str.Substring(j, 1);

            if ((one == two) && (i != j))

            return false;

         }

      }

      return true;

   }

   static void Main(string[] args) {

      Demo d = new Demo();

      bool b = d.CheckUnique("amit");

      Console.WriteLine(b);

      Console.ReadKey();

   }

}

输出结果

True

以上是 C#中字符串的逻辑运算符 的全部内容, 来源链接: utcz.com/z/348905.html

回到顶部