Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Logical Operators บน String ใน C#

Logical Operators ที่ใช้กับ String ใน C# มีดังนี้:

Operatorคำอธิบายตัวอย่าง
&&Logical AND: ทั้งสอง Operand ต้องไม่เป็นศูนย์ (True)(A && B) is false
||Logical OR: Operand ใด Operand หนึ่งไม่เป็นศูนย์ (True)(A || B) is true
!Logical NOT: กลับสถานะ Logic ของ Operand!(A && B) is true

ตัวอย่าง: ใช้ Logical AND ตรวจสอบตัวอักษรซ้ำใน String

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();
    }
}

Output

True