Logical Operators on String in C#


The following are the logical operators that you can use on Strings in C#.

OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non zero then condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands is non zero then condition becomes true.(A || B) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(A && B) is true.

Let us see an example showing how to use logical AND operator on strings −

Example

 Live Demo

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

Updated on: 21-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements