Understanding C# Strings

string c sharpStrings are sequence of characters. The System.String data type is used to represent a string. The string is a reference type object. C# string is a built in data type. The simplest way to construct a string is to use a string literal. The string class in C# represents a string.

string str = “C# Strings are collection of characters”;

Where, str is initialized to the character sequence.

Learn fundamentals of C# Programming with a course at Udemy.com

Consider an example for creating four string variables containing name, age, grade and average.

c#string1

Output for the code is as shown below:

c#string2

Creating string from a char array in C#

There are several constructors that can be overloaded by providing input as array of characters or bytes. The code snippet to create a string from the char array is as shown below:

c#string3

The output for the code is as shown below:

c#string4

Operations on Strings

The string class contains several methods that operate on strings. You can manipulate data using these methods.

1) Split: Split method is used to split parts of a string. An example of split method is as shown below:

c#string5

The output for the code is as shown below:

c#string6

2) Search: Search method is used to search data for substrings. The IndexOf method contains sub methods for performing operations. The methods and their description are as mentioned below:

MethodDescription
IndexOfIt is used to find the first index of the char argument
IndexOfAnyIt is used to find the first index of any of the char arguments. It returns -1 if none item is found in the search
LastIndexOfIt is the last index of the char argument. It returns -1 if none item is found in the search
LastIndexOfAnyIt finds the first index of any char arguments. It returns -1 if none is found

 

The code example for the search method is as shown below:

c#string7

The output for the code is as shown below:

c#string8

3) Concat: Concat method is used to combine strings to form a single large string value. The strings are appended in the output. The code sample for concat is shown below:

c#string9

The output for the code is as shown below:

c#string10

4) Insert and Remove: The user can insert a string at any position in an existing string. The characters of a string can be removed from any position in the string. The code sample for the insert and remove methods are as shown below:

c#string11

The output is:

c#string12

5) Length: Each string has a specific length. The length property is used to count the length of the string. The code sample for counting the length of a string is as shown below:

c#string13

The output for the code is as shown below:

c#string14

6) Substring: You can extract a substring from any string using the substring method. The string can be truncated using the truncate method. The code sample for the substring method is as shown below:

c#string15

The output for the code is as shown below:

c#string16

7) Equals and compare: The String.Equals method is used to test for the equality of two strings. The compare method is used to sort the order of strings. The order of the strings is checked using the ASCII sort concept. The code sample for equals and compare methods are as shown below:

c#string17

The output for the code is as shown below:

c#string18

8) Starts, ends: The StartsWith method is used to test first part of the string. The EndsWith method is used to test the end part of the string. The code sample for the methods is as shown below:

c#string19

The output for the code is as shown below:

c#string20

 

c#string21

The output for the code is as shown below:

c#string22

9) Lower, Upper: You can modify the characters to lower and upper case by using ToLower and ToUpper methods. The code sample for the methods is as shown below:

c#string23

The output for the code is as shown below:

c#string24

10) Null and Empty string: The strings are initialized to null values or null references. Empty string has zero characters present in it. The code sample to demonstrate the methods is as shown below:

c#string25

The output for the code is as shown below:

c#string26

Learn more about C# strings at Udemy.com.

Strings Are Immutable

The contents of the string are immutable. It means that once the string is created, the character sequence cannot be altered. However, you should notice that the string contents can be changed. Consider an example where the character of the string is replaced.

c#string27

The value in the original string s1 is destroyed. The new data is allocated containing the result of the replace operation. The s1 variable is set to the new string data. The strings are immutable internally and does not affect the performance of the code.

StringBuilder class in C#

When the user adds a new method from the string class, a new object is created in computer memory. If repeated operations on the string are performed, the overhead is increased. To overcome this situation, the StringBuilder class is used.

The example of instantiating the StringBuilder class is shown below:

c#string28

There are several methods that helps to modify the contents of the StringBuilder as shown below:

1) Append: It is used to append the information or a string representation of the object at the end of the string. The example for the method is as shown below:

c#string29

The output for the code is as shown below:

c#string30

2) Insert: The Insert method adds a string or object to a specified position in the StringBuilder object. The code sample to demonstrate the method is as shown below:

c#string31

The output is as shown below:

c#string32

3) Remove: The Remove method is used to remove the number of characters specified from the StringBuilder object. The code sample for the method is as shown below:

c#string33

The output for the code is as shown below:

c#string34

4) Replace: The Replace method is used to replace characters within the StringBuilder object with the specified character. The code sample for the method is as shown below:

c#string35

The output for the code is as shown below:

c#string36

Learn more in depth on C# programming and strings with a class at Udemy.com