Home

Operations on Strings

 

String Concatenation

One of the routine operations you can perform on two strings consists of adding one to another, that is, putting one string to the right of another string, to produce a new string made of both. There are two techniques you can use.

To add one string to another, you can use the addition operator as done in arithmetic. Here is an example:

using System;

class Program
{
    static int Main()
    {
        string strNeed = "Needs";
        string strRepair = "Repair";
        string strAddition = strNeed + strRepair;

        Console.WriteLine(strAddition);

        return 0;
    }
}

This would produce:

NeedsRepair
Press any key to continue . . .

In the same way, you can add as many strings as necessary using +. Here is an example:

using System;

class Program
{
    static int Main()
    {
        string strFirstName  = "Alexander";
        string strMiddleName = "Patrick";
        string strLastName   = "Katts";
        string strFullName   = strFirstName + " " + 
                               strMiddleName + " " + 
                               strLastName;

        Console.WriteLine("First Name:  " + strFirstName);
        Console.WriteLine("Middle Name: " + strMiddleName);
        Console.WriteLine("Last Name:   " + strLastName);
        Console.WriteLine("Full Name:   " + strFullName + "\n");

        return 0;
    }
}

This would produce:

First Name:  Alexander
Middle Name: Patrick
Last Name:   Katts
Full Name:   Alexander Patrick Katts

Press any key to continue . . .

Besides the addition operator, to formally support string concatenation, the String class provides the Concat() method that is overloaded in various versions. One of the versions of this method takes two String arguments. Its syntax is:

public static string Concat(string str1, string str2);

This versions takes two strings that should be concatenated. The method returns a new string as the first added to the second. Two imitations of this version use the following versions:

public static string Concat(string str0, string str1, string str2);
public static string Concat(string str0, string str1, string str2, string str3);

In each case, the method takes the number of strings and adds them.

Strings Comparisons

 

Introduction

String comparison consists of examining the characters of two strings with a character of one string compared to a character of the other string with both characters at the same positions. To support this operation, the String class is equipped with the Compare() method that is overloaded in many versions. One of the versions uses the following syntax:

public static int Compare(string string1, string  String2);

This method is declared static and it takes two arguments. When it starts, the first character of the first argument is compared to the first character of the second string. Alphabetically, if the first character of the first string has a lower alphabetical index than the first character of the second, this method returns a negative value. If the first character of the first string has a higher alphabetical index than the first character of the second, this method returns a positive value. If the first characters of both strings are the same, the method continues with the second character of each string. If both strings have the exact same characters, the method returns 0. This can be resumed as follows. The method returns

  • A negative value if string1 is less than string2
  • 0 if string1 and string2 are equal
  • A positive value if string1 is greater than string2

Here is an example:

using System;

class Program
{
    static int Main()
    {
        string FirstName1 = "Andy";
        string LastName1 = "Stanley";
        string FirstName2 = "Charles";
        string LastName2 = "Stanley";

        int Value1 = string.Compare(FirstName1, FirstName2);
        int Value2 = string.Compare(FirstName2, FirstName1);
        int Value3 = string.Compare(LastName1, LastName2);

        Console.WriteLine("The result of comparing " + 
                          FirstName1 + " and " + FirstName2 +
                          " is\t" + Value1.ToString());
        Console.WriteLine("The result of comparing " + 
                          FirstName2 + " and " + FirstName1 +
                          " is\t" + Value2.ToString());
        Console.WriteLine("The result of comparing " +
                          LastName1 + " and " + LastName2 + 
                          " is\t" + Value3.ToString() + "\n");

        return 0;
    }
}

This would produce:

The result of comparing Andy and Charles is     -1
The result of comparing Charles and Andy is     1
The result of comparing Stanley and Stanley is  0

Press any key to continue...

When using this version of the string.Compare() method, the case (upper or lower) of each character is considered. If you don't want to consider this factor, the String class proposes another version of the method. Its syntax is:

public static int Compare(string String1, string String2, bool ignoreCase);

The third argument allows you to ignore the case of the characters when performing the comparison.

String Equality

In the previous section, we saw that the indexed-equivalent characters of two strings can be compared to know whether one is lower or higher than the other's. If you are only interested to know whether two strings are equivalent, you can call the Equals() method of the String class. It is overloaded with various versions. Two versions use the following syntaxes:

public override bool Equals(object obj);
public bool Equals(string value);

When calling one of these versions, use an Object object or a String variable that calls it. The method takes one argument. The variable that calls the method is compared to the value passed as argument. If both values are the exact same, the method returns true. The comparison is performed considering the case of each character. If you don't want to consider the case, use the following version of the method:

public bool Equals(string value, StringComparison comparisonType);

An alternative to the second syntax is to use a static version of this method whose syntax is:

public static bool Equals(string a, string b);

This method takes two String arguments and compares them. If they are the same, the method returns true. This method considers the cases of the characters. If you don't want this factor taken into consideration, use the following version of the method:

public static bool Equals(string  a, string  b, StringComparison comparisonType);

Working With Sub-Strings

 

Introduction

A sub-string is a section or part of a string. To create a sub-string, you first need a string and can retrieve one or more values from it. To support this, the String class is equipped with the Substring() method that is overloaded in two versions. The syntax of one is:

public string Substring(int startIndex);

The integer argument specifies the position of the first character from the variable that called the method. The return value is a new String that is made of the characters from startIndex to the end of the string.

Sub-String Creation

Probably the most consistent way to create a string is to control the beginning and end retrieved from the original string. To support this, the String class is equipped with another version of the Substring() method. Its syntax is:

public string  Substring(int startIndex, int length);

The first argument specifies the index of the character to start from the String variable that calls this method. The second argument specifies the length of the string.

 

 

Previous Copyright © 2006 FunctionX, Inc.