C# String Operators


There are two string operators in C#: Equality and Inequality.

Example

Let us see an example of Equality operator −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      string str1 = "Amit";
      string str2 = " ";
      Console.WriteLine("Is string1 null or empty? = "+string.IsNullOrEmpty(str1));
      Console.WriteLine("Is string2 null or empty? = "+string.IsNullOrEmpty(str2));
      Console.WriteLine("Is string1 equal to str2? = "+str1 == str2);
   }
}

Output

Is string1 null or empty? = False
Is string2 null or empty? = False
False

Example

Let us now see an example of C# Inequality operator −

 Live Demo

using System;
public class Demo {
   public static void Main() {
      string str1 = "abcdef";
      string str2 = "abcdef";
      Console.WriteLine("Is string1 null or empty? = "+string.IsNullOrEmpty(str1));
      Console.WriteLine("Is string2 null or empty? = "+string.IsNullOrEmpty(str2));
      bool val = str1 != str2;
      Console.WriteLine("Is str1 not equal to str2? = "+val);
   }
}

Output

Is string1 null or empty? = False
Is string2 null or empty? = False
Is str1 not equal to str2? = False

Updated on: 02-Dec-2019

438 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements