String class, String declaration and string constructors...

String class String

The basic type for working with strings in C # is a string type that specifies variable-length strings. Above the rows - objects of this class - a wide range of operations is defined, corresponding to the modern idea of ​​how a string type should be constructed.

Declaring strings and constructors of the string class

Objects of the String class are declared as all other objects of simple types with explicit or deferred initialization, with an explicit or implicit class constructor call. Most often when you declare a string variable, the constructor is not explicitly called, and initialization is specified by a string constant. But the String class has a lot of constructors. They allow you to build a string using:

• a character repeated a predetermined number of times;

• array of characters char [];

• parts of the array of characters.

String operations

The following operations are defined above the lines:

• Assignment (=);

• equivalence check (==) and (! =);

• concatenation, or the concatenation of strings (+);

• Get the character by index ([]).

Because string is a reference type, the result of the assignment is a reference to a constant string stored in the heap. Several string variables can be associated with the same string constant in the heap. But these variables are not aliases - different names of the same object. The fact is that string constants do not change in the heap, so when one of the variables gets a new value, it is associated with a new constant object in the heap. The remaining variables retain their connections. For the programmer, this means that the semantics of assigning strings is analogous to the semantics of meaningful assignment.

Unlike other reference types, operations that verify equivalence compare string values, not references. These operations are performed as if they are over significant types.

Binary operation + concatenates two lines, assigning the second line to the end of the first.

The ability to take an index when working with strings makes it possible to work with a string, as with an array and get every symbol of the index. The string characters are char-style and are read-only, but not for writing. Below is an example in which the following operations are performed on lines:

public void TestOpers () {// operations on strings

string s1 = ABC & quot ;, s2 = CDE & quot ;;

string s3 = s1 + s2;

bool b1 = (s1 == s2);

char ch1 = s1 [0], ch2 = s2 [0];

Console.WriteLine ( s1 = {0}, s2 = {1}, b1 = {2}, + ch1 = {3}, ch2 = {4} s1, s2, b1, ch1, ch2);

s2 = s1;

S = (s1! = s2); ch2 = s2 [0];

Console.WriteLine ( s1 = {0}, s2 = {1}, b1 = {2}, + ch1 = {3}, ch2 = {4} s1, s2, b1, ch1, ch2);

// Unchangeable values ​​

s1 = Zenon & quot ;;

// s1 [0] = 'L';


Table 7.4

Basic static methods of the String class




comparison of two lines; You can compare both strings and substrings; it is possible to consider or not take into account the register, etc.


comparison of two lines; character codes are compared


string concatenation; the method is overloaded, allows for the concatenation of an arbitrary number of rows


creating a copy of the line


formatting, in accordance with the specified format specifications, is similar to the Console.Write () method with the format


join a string array into a single string; when separating between elements of an array, delimiters are inserted

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)