Compares two specified String objects. | |
Compare(String, String, Boolean) | Compares two specified String objects, ignoring or respecting their case. |
Compare(String, String, StringComparison) | Compares two specified String objects. Also specifies whether the comparison uses the current or invariant culture, honors or respects case, and uses word or ordinal sort rules. |
Compare(String, String, Boolean, CultureInfo) | Compares two specified String objects, ignoring or respecting their case, and using culture-specific information for the comparison. |
Compare(String, Int32, String, Int32, Int32) | Compares substrings of two specified String objects. |
Compare(String, Int32, String, Int32, Int32, Boolean) | Compares substrings of two specified String objects, ignoring or respecting their case. |
Compare(String, Int32, String, Int32, Int32, StringComparison) | Compares substrings of two specified String objects. |
Compare(String, Int32, String, Int32, Int32, Boolean, CultureInfo) | Compares substrings of two specified String objects, ignoring or respecting their case, and using culture-specific information for the comparison. |
Alternatively, you can use the CompareTo()
instance method, like this:
Console.WriteLine(str1.CompareTo(str2)); // 1; str1 is greater than str2
Console.WriteLine(str2.CompareTo(str1)); // -1; str2 is less than str1
Note that comparisons made by the CompareTo()
instance method are always case sensitive.
The String
class in the .NET Framework provides a number of methods that enable you to create or concatenate strings.
The most direct way of concatenating two strings is to use the '+
' operator, like this:
string str1 = 'Hello ';
string str2 = 'world!';
string str3 = str1 + str2;
Console.WriteLine(str3); //---Hello world!---
The String.Format()
static method takes the input of multiple objects and creates a new string. Consider the following example:
string Name = 'Wei-Meng Lee';
int age = 18;
string str1 = string.Format('My name is {0} and I am {1} years old', Name, age);
//---str1 is now 'My name is Wei-Meng Lee and I am 18 years old'---
Console.WriteLine(str1);
Notice that you supplied two variables of string and int type and the Format()
method automatically combines them to return a new string.
The preceding example can be rewritten using the String.Concat()
static method, like this:
string str1 =
string.Concat('My name is ', Name, ' and I am ', age, ' years old');
//---str1 is now 'My name is Wei-Meng Lee and I am 18 years old'---
Console.WriteLine(str1);
In .NET, all string objects are
For example, the Insert()
instance method inserts a string into the current string and returns the modified string:
str1 = str1.Insert(10, 'modified ');
In this statement, you have to assign the returned result to the original string to ensure that the new string is modified.
The String.Join()
static method is useful when you need to join a series of strings stored in a string array. The following example shows the strings in a string array joined using the Join()
method:
string[] pts = { '1,2', '3,4', '5,6' };
string str1 = string.Join('|', pts);
Console.WriteLine(str1); //---1,2|3,4|5,6---
To insert a string into an existing string, use the instance method Insert(), as demonstrated in the following example:
string str1 = 'This is a string';
str1 = str1.Insert(10, 'modified ');
Console.WriteLine(str1); //---This is a modified string---
The Copy()
instance method enables you to copy part of a string into a char array. Consider the following example:
string str1 = 'This is a string';
char[] ch = { '*', '*', '*', '*', '*', '*', '*', '*' };
str1.CopyTo(0, ch, 2, 4); Console.WriteLine(ch); //---**This**---
The first parameter of the CopyTo()
method specifies the index of the string to start copying from. The second parameter specifies the char array. The third parameter specifies the index of the array to copy into, while the last parameter specifies the number of characters to copy.
If you need to pad a string with characters to achieve a certain length, use the PadLeft()
and PadRight()
instance methods, as the following statements show:
string str1 = 'This is a string'; string str2;
str2 = str1.PadLeft(20, '*');