Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

26

Part I

INTRODUCTION TO C#

 

 

 

Strings

C# provides you with a String type. In C and C++, an array of characters is called a string. String is not a class in C and C++, therefore, working with strings is a problem in C and C++. Simple operations, such as comparing or adding two strings, require a lot of programming. To provide the programmers with a solution to this problem, C# created a String class.

Initializing Strings

You can initialize a string by using the string keyword. To initialize a string, string1, use the following statement:

string string1 = “Hello World”;

The previous sample code declares a string, string1, and initializes it with a value

“Hello World”.

The assignment of a value to a string takes place by the reference type. When you declare a string, an object of the String class is created and placed on the heap. This object of the String class has the reference to the memory location where the string is stored.

Working with Strings

The String class in C# is in the System namespace. String is a class and has several methods associated with it. You can use these methods to perform operations on strings. The commonly used methods in the String class are as follows:

Compare(). The Compare() method is used to compare two strings.

Format(). You can use the Format() method to format the values in a

string. The Format() method allows you to specify formatting for each value in a string.

Trim(). The Trim() method in C# deletes the extra spaces in a string. The Trim() method can be used to delete both the leading and trailing spaces.

ToUpper(). To change the capitalization of the elements in a string, you can use the ToLower() or ToUpper() methods. The ToLower() method

C# BASICS

Chapter 2

 

27

 

 

 

 

 

converts the elements of the string to lowercase. Similarly, you can convert the string to uppercase by using the ToUpper() method.

Split(). Working with large strings can be a problem. Therefore, the System.String class provides the Split() method that can be used to break a string into several small strings. You can specify a character from where the string should be split. The Split() method breaks the string into substrings at each instance of the given character. The substrings created by the Split() method are stored in the form of an array.

IndexOf(). The IndexOf() method is used to locate a character or substring in the main string. The IndexOf() method returns the index of the first instance of the specified character in a string. Similarly, to locate the last occurrence of a character or substring, you can use the LastIndexOf() method.

IndexOfAny(). If you need to know the index of the first occurrence of any one of a set of characters in a string, you can use the IndexOfAny() method. Similarly, the LastIndexOfAny() method is used to locate the index of the last occurrence of any one of a set of characters in a string.

Replace(). To replace all occurrences of a character or a substring in a string by another character or substring, you use the Replace() method.

Simple operations on a string, such as adding or concatenating two strings, can be performed using a (+) operator. You do not require a method for concatenating two strings. For example, to add string1 and string2, you first need to initialize the two strings. You can then use the (+) operator to add the two strings and initialize their value to another string, string3. The code sample that follows displays this.

string string1 = ‘John ‘; string string2 = ‘Floyd’;

string string3 = string1 + string2;

The value of string3 in the previous sample is John Floyd.

You have learned about performing simple operations on a string. Now look at the various statements and expressions provided by C#. You can use these statements and expressions to perform specific operations on variables.