Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Logical Operators on String in C#
Logical operators in C# are used to perform logical operations on boolean expressions. When working with strings, these operators are commonly used to evaluate string conditions and create compound boolean expressions for string validation and comparison logic.
Logical Operators
| Operator | Description | Example |
|---|---|---|
| && | Called Logical AND operator. Returns true if both operands are true. | (str != null && str.Length > 0) |
| || | Called Logical OR operator. Returns true if at least one operand is true. | (str == "" || str == null) |
| ! | Called Logical NOT operator. Reverses the logical state of its operand. | !(string.IsNullOrEmpty(str)) |
Using Logical AND (&&) with Strings
The logical AND operator is commonly used to check multiple string conditions simultaneously. Both conditions must be true for the entire expression to evaluate to true −
using System;
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 result1 = d.CheckUnique("amit");
bool result2 = d.CheckUnique("hello");
Console.WriteLine("'amit' has unique characters: " + result1);
Console.WriteLine("'hello' has unique characters: " + result2);
}
}
The output of the above code is −
'amit' has unique characters: True 'hello' has unique characters: False
Using Logical OR (||) with Strings
The logical OR operator returns true if at least one condition is true. It's useful for checking alternative string conditions −
using System;
public class StringValidator {
public bool IsEmptyOrNull(string str) {
return (str == null || str == "");
}
public bool IsValidName(string name) {
return (name != null && name.Length >= 2) && !(name == "" || name.All(char.IsWhiteSpace));
}
static void Main(string[] args) {
StringValidator validator = new StringValidator();
string[] testStrings = { null, "", "A", "John", " " };
foreach (string test in testStrings) {
bool isEmpty = validator.IsEmptyOrNull(test);
bool isValid = validator.IsValidName(test);
Console.WriteLine($"String: '{test}' | Empty/Null: {isEmpty} | Valid Name: {isValid}");
}
}
}
The output of the above code is −
String: '' | Empty/Null: True | Valid Name: False String: '' | Empty/Null: True | Valid Name: False String: 'A' | Empty/Null: False | Valid Name: False String: 'John' | Empty/Null: False | Valid Name: True String: ' ' | Empty/Null: False | Valid Name: False
Using Logical NOT (!) with Strings
The logical NOT operator reverses the boolean result of an expression. It's commonly used to negate string validation conditions −
using System;
public class Program {
static void Main(string[] args) {
string[] passwords = { "abc", "Password123", "", "12345678" };
foreach (string password in passwords) {
bool isNotEmpty = !string.IsNullOrEmpty(password);
bool isNotShort = !(password != null && password.Length < 6);
bool isSecure = isNotEmpty && isNotShort;
Console.WriteLine($"Password: '{password}'");
Console.WriteLine($"Not empty: {isNotEmpty}");
Console.WriteLine($"Not short: {isNotShort}");
Console.WriteLine($"Secure: {isSecure}");
Console.WriteLine("---");
}
}
}
The output of the above code is −
Password: 'abc' Not empty: True Not short: False Secure: False --- Password: 'Password123' Not empty: True Not short: True Secure: True --- Password: '' Not empty: False Not short: True Secure: False --- Password: '12345678' Not empty: True Not short: True Secure: True ---
Conclusion
Logical operators (&&, ||, !) are essential for creating complex string validation logic in C#. They allow you to combine multiple string conditions efficiently, enabling robust input validation and string processing in your applications.
