Understanding C# Strings
Strings 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.
Output for the code is as shown below:
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:
The output for the code is as shown below:
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:
The output for the code is as shown below:
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:
Method | Description |
IndexOf | It is used to find the first index of the char argument |
IndexOfAny | It is used to find the first index of any of the char arguments. It returns -1 if none item is found in the search |
LastIndexOf | It is the last index of the char argument. It returns -1 if none item is found in the search |
LastIndexOfAny | It 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:
The output for the code is as shown below:
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:
The output for the code is as shown below:
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:
The output is:
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:
The output for the code is as shown below:
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:
The output for the code is as shown below:
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:
The output for the code is as shown below:
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:
The output for the code is as shown below:
The output for the code is as shown below:
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:
The output for the code is as shown below:
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:
The output for the code is as shown below:
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.
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:
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:
The output for the code is as shown below:
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:
The output is as shown below:
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:
The output for the code is as shown below:
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:
The output for the code is as shown below:
Learn more in depth on C# programming and strings with a class at Udemy.com
Recommended Articles
Top courses in C# (programming language)
C# (programming language) students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.