Table of contents >> Strings And Text Processing > Other string methods
Low importance article, or discussion

There are a number of other string methods that you might find useful. They are:

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
27
using System;
 
namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "a";
            string b = "b";
            
            string 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.ReadLine();
        }
    }
}

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
27
using System;
 
namespace HelloWorld
{
    class Program
    {
        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 (b == a)
                Console.WriteLine("a == b = true");
            
            Console.ReadLine();
        }
    }
}

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.