String Operators

The System.String class overloads several operators to facilitate common string operations.

Equality Operators

These operators are used to compare two strings for equality.

operator ==

static bool operator == (string? left, string? right);

Determines whether two specified strings have the same value.

Note: This is an ordinal (case-sensitive) comparison. For culture-sensitive or case-insensitive comparisons, use the String.Equals method with appropriate arguments.

operator !=

static bool operator != (string? left, string? right);

Determines whether two specified strings have different values.

Note: This is an ordinal (case-sensitive) comparison. For culture-sensitive or case-insensitive comparisons, use the String.Equals method with appropriate arguments.

Concatenation Operator

The + operator is used to combine two or more strings into a single string.

operator +

public static string operator + (string? strA, string? strB);

Concatenates two strings, or a string and the string representation of another value.

Example


string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
// fullName will be "John Doe"

int year = 2023;
string message = "Current year: " + year;
// message will be "Current year: 2023"