BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# String Testing Functions

The twenty-fourth part of the C# Fundamentals tutorial completes the initial examination of the string manipulation functionality provided by the String class. This article investigates methods available for testing the contents of strings.

Retrieving a String's Length

It is often necessary to check the length of a string, particularly to validate a user's input. The String class provides a property named Length that gives us this information. The property returns an integer representing the number of characters present.

string animal = "Dog";
int result;

result = animal.Length;                     // result is 3
result = "Elephant".Length;                 // result is 8

NB: The Length property is efficient to execute. When checking if a string is empty, it is more efficient to check for zero length than to compare the string value to String.Empty or "".

Locating Text Within a String

In an earlier article we examined the Substring method, which allows the extraction of characters from a specified position in a string. The reverse of this is to specify a series of characters and identify the position at which that sub-string appears within a string. This can be achieved with the IndexOf and LastIndexOf methods.

IndexOf takes a parameter containing a string to search for. If this exists within the main string the position of the first occurrence is returned. This position is known as the index and is an integer indicating the number of characters between the start of the string and the found text. If the search term appears at the start of the string the index is zero. Should the sub-string not exist within the main string the method returns -1.

LastIndexOf is similar to IndexOf. However, instead of returning the index of the first occurrence of the search term, LastIndexOf returns the position of the last occurrence.

string phrase = "The quick brown fox jumps over the lazy dog.";
int result;

result = phrase.IndexOf("brown");           // result is 10
result = phrase.LastIndexOf("dog");         // result is 40
result = phrase.IndexOf("green");           // result is -1
result = phrase.LastIndexOf("blue");        // result is -1

Specifying a Limited Search

IndexOf and LastIndexOf can be limited further by specifying a start position for the search. With IndexOf only occurrences of the specified sub-string at or to the right of this position are found. When using LastIndexOf, only matching sub-strings to the left of this position are located.

string phrase = "The quick brown fox jumps over the lazy dog.";
int result;

result = phrase.IndexOf("he");              // result is 1
result = phrase.IndexOf("he", 1);           // result is 1
result = phrase.IndexOf("he", 2);           // result is 32
result = phrase.LastIndexOf("he");          // result is 32
result = phrase.LastIndexOf("he",33);       // result is 32
result = phrase.LastIndexOf("he",32);       // result is 1

The search can be limited further with a third parameter. This parameter is an integer that indicates the maximum number of characters that may be examined. If a match does not exist within the range of characters the result returned is -1.

string phrase = "The quick brown fox jumps over the lazy dog.";
int result;

result = phrase.IndexOf("brown");           // result is 10
result = phrase.IndexOf("brown", 5, 4);     // result is -1
result = phrase.LastIndexOf("fox");         // result is 16
result = phrase.LastIndexOf("he", 25, 5);   // result is -1

Simple Container Tests

IndexOf and LastIndexOf are useful when determining the exact position of search terms within strings. However, sometimes it is enough to simply know that a sub-string exists. The String class provides several methods to perform this test.

StartsWith and EndsWith

StartsWith and EndsWith allow you to determine if a string starts or ends with a specific series of characters. Both methods accept an argument containing the string to match and return a Boolean value indicating the result. These methods are case-sensitive.

string phrase = "The quick brown fox jumps over the lazy dog.";
bool result;

result = phrase.StartsWith("The quick");    // result is true
result = phrase.StartsWith("lazy dog.");    // result is false
result = phrase.EndsWith("lazy dog.");      // result is true
result = phrase.EndsWith("The quick");      // result is false

.NET 2.0 Contains Method

The final string function considered in this article is Contains. Introduced in version 2.0 of the .NET framework, it operates in a similar manner to StartsWith and EndsWith but checks if the specified search term exists at any position within the main string.

string phrase = "The quick brown fox jumps over the lazy dog.";
bool result;

result = phrase.Contains("brown fox");      // result is true
result = phrase.Contains("lazy fox.");      // result is false
11 December 2006