In C#, String is the series of characters or array of characters that represents a text. It is of a reference type (Object) in c#. Various operations are performed on a string such as concatenation, comparison, getting substring, search, trim, replacement, etc.
- It is a reference type.
- It’s immutable that is its state cannot be changed.
- It can contain nulls.
- It overloads the operator(==).
Comparing string and String
string and String are the same in C#, the keyword string is an alias of System.String. Both are equivalent and we can use any convention because the string
class inherits the properties and methods of the System.String
class.
Declaring string using string and System.String
class.
1 2 3 | string str1 = "Simple2code"; // uses string keyword String str2 = "Simple2code"; // uses System.String clas |
A variable of the string type can be declared and assign string literal as given below.
1 2 3 | string ch = "S"; string str1 = "Simple2code"; string str2 = "This is simple2code.com"; |
String example in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System; namespace StringProgram { class Program { static void Main(string[] args) { //using array of character char[] ch = { 's', 'i', 'm', 'p', 'l', 'e' }; string str = new string(ch); Console.WriteLine(str); // using "System.String" class System.String name = "Simple"; Console.WriteLine("Name: " + name); // using an alias of System.String class String roll = "43"; Console.WriteLine("Roll: " + roll); // using string string score = "80"; Console.WriteLine("Marks: " + score); } } } |
Output:
simple
Name: SimpleRoll: 43
Marks: 80
C# String Properties
Chars | returns a char object at a specified position in the current string object. |
Length | returns the length of a specified string |
C# String methods
We can perform an operation on strings in order to manipulate them according to the need. An to do that there are few methods present in C#. The following table shows the most commonly used methods in string.
Methods | Description |
---|---|
Compare(String, String) | It is used to compare two strings and returns integer value as an output. |
Concat( | It is used to concatenate the two specified strings. |
Contains(String) | It is used to check whether a specified character/string exists or not in the string value. |
Copy(String) | It creates a new instance of String with the same value as a specified String. |
EndsWith(String) | It is used to check whether the specified character is the last character of the string or not. |
Equals(String, String) | It is used to compare two strings. It returns a Boolean value as output. |
GetHashCode() | It is used to get the HashValue of the specified string. |
IndexOf() | It is used to get the index position of the first occurrence of the specified character. |
ToLower() | It is used to transform the specified string into the lower case. |
ToUpper() | It is used to transform the specified string into the upper case. |
ToString() | It returns the instance of a String. |
Insert() | It is used to insert the string/character in the string at the specified position. |
Remove() | It is used to delete all the characters from beginning to specified index positions. |
Replace() | It is used to replace the character. |
Split() | It is used to split the string at the specified value. |
StartsWith() | It is used to check whether the specified character is the starting character of the string or not. |
Substring() | It is used to find the substring from a specified string. |
ToCharArray() | It is used to convert a string into a char array. |
Trim() | It is used to remove the extra whitespaces from the beginning and end of the specified string. |
Let us go through some of the examples of the above methods mentioned in C#.
Comparing String in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; namespace StringProgram { class CompString { static void Main(string[] args) { string str1 = "This is a website"; string str2 = "This is simple2code.com"; if (String.Compare(str1, str2) == 0) Console.WriteLine("Strings are equal."); else Console.WriteLine("Strings are not equal."); } } } |
//OutputStrings are not equal.
Concatinate Stringin C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System; namespace StringProgram { class ConcatString { static void Main(string[] args) { string str1 = "This is "; string str2 = "simple2code.com"; Console.WriteLine(String.Concat(str1, str2)); } } } |
//OutputThis is simple2code.com
String Contains method in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System; namespace StringProgram { class ContainString { static void Main(string[] args) { string str1 = "This is simple2code.com"; string str2 = "simple2code.com"; if (str1.Contains(str2)) Console.WriteLine("FOUND!"); //or You can directly search the letters if (str1.Contains("is")) Console.WriteLine("FOUND!"); } } } |
//OutputFOUND!
FOUND!
Substring method in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System; namespace StringProgram { class SubString { static void Main(string[] args) { string str1 = "This is simple2code.com"; string subStr = str1.Substring(8); Console.WriteLine(subStr); } } } |
//Outputsimple2code.com
Special Characters
As we have seen till now the string must be within the quotes. Now there might be cases where the string may need a single quote or double quote to form sentences. Example, John’s book, It’s alright, etc.
Now to use quotes within the string quotes, we need to use backslash escape character. With the help of this, we can turn the special character into a string character.
Escape character | Result |
---|---|
\' | ‘ |
\" | “ |
\\ | \ |
Let us go through an example to demonstrate the escape character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; namespace StringProgram { class SpecialChar { static void Main(string[] args) { string str1 = "This is \"simple2code.com\"."; string str2 = "It\'s allright."; Console.WriteLine(str1); Console.WriteLine(str2); } } } |
//Output:This is "simple2code.com".
It's allright.
As you can see in the output, the result is with the quotes where we inserted in the program. This way you can apply special characters in a string.