Logical Operators on String in C#

CsharpProgrammingServer Side Programming

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

&&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 −


 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");


Published on 13-Sep-2018 08:43:57