There are a number of other string methods that you might find useful. They are:
Compare(), CompareTo(), CompareOrdinal() – determines the sort order of strings. It checks if one string is ordered before another when in alphabetical order, whether it is ordered after, or is equivalent. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; namespace HelloWorld { class Program { private static void Main(string[] args) { string a = "a"; string b = "b"; int c = string.Compare(a, b); Console.WriteLine(c); // -1 c = string.CompareOrdinal(b, a); Console.WriteLine(c); // 1 c = a.CompareTo(b); Console.WriteLine(c); // -1 c = b.CompareTo(a); Console.WriteLine(c); // 1 Console.Read(); } } } |
Equals() – compares strings. It is not the same as the Compare() and CompareTo() functions. Equals tests strings for equality. It is invoked with the function name Equals() or with the equality operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; namespace HelloWorld { class Program { private static void Main(string[] args) { string a = "a" + 1; string b = "a" + 1; // Compare a to b using instance method on a. if (a.Equals(b)) Console.WriteLine("a.Equals(b) = true"); // Compare b to a using instance method on b. if (b.Equals(a)) Console.WriteLine("b.Equals(a) = true"); // Compare a to b with equality operator if (a == b) Console.WriteLine("a == b = true"); Console.Read(); } } } |
IsNullOrEmpty() – indicates whether the specified string is null or an String.Empty string.
IsNullOrWhiteSpace() – indicates whether a specified string is null, empty, or consists only of white-space.
Join() – merges together into a single string the elements of a list or array of strings, using a specified separator.
Clone() – returns a reference to this instance of string.
Insert() – returns a new string in which a specified string is inserted at a specified index
Tags: clone, compare, equals, insert, join, string operations, string variable type