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

csharpprogrammingserver side programming更新于 2025/8/13 23:07:17

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

运算符描述示例
&&称为逻辑"与"运算符。如果两个操作数都非零,则条件为真。(A && B) 为假。
||称为逻辑"或"运算符。如果两个操作数中有一个非零,则条件为真。(A || B) 为真。
!称为逻辑"非"运算符。用于反转其操作数的逻辑状态。如果条件为真,则逻辑非运算符将返回假。!(A && B) 为真。

让我们看一个例子,展示如何在字符串上使用逻辑与运算符 −

示例

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

相关文章