- 1 The strings in C#
- 2 How to create a string?
- 3 An example of a C# string
- 4 A string example by class constructor
- 5 Example of string length property
- 6 A few string methods and examples
- 6.1 String split method example
- 6.2 The string replace method example
- 6.3 C# trim method example
- 6.4 C# IndexOf method example
- 6.5 The string compare method
- 6.6 The string CompareTo method
- 6.7 The Equals method
- 6.8 C# clone method example
- 6.9 C# contains method example
- 6.10 The GetHashCode method example
- 6.11 The join method example
- 7 Related
The strings in C#
A string is a sequence of characters where each character is a Unicode character. For example, “This is a C# programming tutorial” is a simple string.
A few main points about C# strings are:
- A string is a type of variable in C# programming.
- Each character in the string is a Unicode character.
- Strings are the sequence of characters.
- C sharp Strings are objects.
- C# has System.String class that enables you to create strings.
- The string (starting with small ‘s’) is an alias of System.String class, that means you can create a string by using the string keyword.
- There is no difference between string and String (capital or small ‘s’)
- The String class has its properties and methods that can be used to perform desired tasks on strings like the length, Concat, Split, Trim, Compare etc.
This tutorial will show you how to create the strings and how to use a few useful methods of the strings.
How to create a string?
You can create the string in C# as follows:
string str = “This is c# string”;
Or
As such, the string is an object, you can create it by using the string class constructor with the new keyword as follows:
string str = new string(array_of_Unicode_characters);
See the example below of using this way to understand it more clearly.
Generally you will use the first method to create the strings.
An example of a C# string
Following is a simple example of creating a string. We will create a string by using the System.String class alias and display it. Click the link or image below to see online demo:
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 csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str;
str = “This is string tutorial!”;
Console.WriteLine(str); // Csharp string example
Console.ReadLine();
}
}
}
|
A string example by class constructor
Following example creates a string by using the String class constructor. See example by clicking the link below:
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 csharp_demo
{
class Program
{
static void Main(string[] args)
{
char[] str_ex = { ‘S’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’ };
string str = new string(str_ex);
Console.WriteLine(str); // Csharp string example
Console.ReadLine();
}
}
}
|
Example of string length property
As we mentioned, strings have its properties and methods. One of the useful property is the length.
The length property returns the number of characters of the current string. Following example shows how to use the string length property.
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
28
29
|
using System;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str;
str = “This is string tutorial!”;
int strlength = str.Length;
Console.WriteLine(“The length of string is: “ + strlength); // Csharp string example
Console.ReadLine();
}
}
}
|
A few string methods and examples
The string class has very useful methods to work with string operations in C# programming. Now, let us look at a few useful methods of the String class.
String split method example
The string split method returns an array of the broken string. The specified string is broken by a given Unicode character. Following example shows how to use the string split method.
In this example, we have created a string and after that, we will break it by a space character. As such it returns an array, for understanding purpose we will display a specific array element of broken string as well as the whole array by using the foreach loop.
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
28
29
30
31
32
33
34
35
36
37
|
using System;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str = “This is String tutorial”;
string[] str_split = str.Split(new char[] { ‘ ‘ });
Console.WriteLine(str_split[2]); // Displaying specific array element
Console.WriteLine(“—————————-“);
foreach (string strspl in str_split)
{
Console.WriteLine(strspl); // Display whole broken string
}
Console.ReadLine();
}
}
}
|
You can see, first we displayed 2nd index element and then used the foreach loop to display whole string, broken by using the split method.
The string replace method example
The string replace C# method is used to replace the given Unicode character(s) in the specified string to matched existing character(s). The replace method returns a string which is replaced by the given characters.
To make it clearer, see the following example of using string replace. In this example, we will replace a word “tutorial” to “Tutorial” by using the replace method.
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 csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str = “This is String tutorial”;
string str_rep = str.Replace(“tutorial”, “Tutorial”);
Console.WriteLine(str_rep);
Console.ReadLine();
}
}
}
|
You can see, both strings are displayed, before and after using string replace method.
C# trim method example
The C# string trim method removes all leading and trailing spaces in the specified string. See the following example of using the trim C# method where we created a string with the leading and trailing spaces.
After that, we displayed string before and after using the trim method.
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 csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str = ” This is String tutorial “;
Console.WriteLine(“Before trim: “ + str);
Console.WriteLine(“After trim: “ + str.Trim());
Console.ReadLine();
}
}
}
|
You can see, all spaces from left and right are removed by using the trim method.
C# IndexOf method example
The IndexOf method is used to search in the string. Basically, IndexOf method finds the first occurrence of the given character and returns position in the integer value, based at zero index.
The following example will make it clear what actually IndexOf method does.
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 csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str = “This is String tutorial”;
int index_of = str.IndexOf(“s”);
Console.WriteLine(“The first occurence of s is: “ + index_of);
Console.ReadLine();
}
}
}
|
You can see, we used ‘s’ in IndexOf method and it returned 3 in the output. As such, IndexOf returns zero-based index value, so the first occurrence of ‘s’ letter is 3rd.
The string compare method
The C# string compare method is used to compare two given strings. The string compare method returns 0 if strings are matched and 1 if the strings are not matched.
Following example shows how to use it.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
using System;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str1, str2;
str1 = “This is String tutorial”;
str2 = “This is String tutorial”;
if (String.Compare(str1,str2)==0)
{
Console.WriteLine(“Strings are matched!”);
}
else if (String.Compare(str1, str2) == 1)
{
Console.WriteLine(“Strings are not matched!”);
}
Console.ReadLine();
}
}
}
|
You can see, as both the strings are same, it returned 0 value.
You can also use CompareTo method as shown below.
The string CompareTo method
You can also use C# CompareTo method to compare two strings. The CompareTo method also returns 0 if the match is true, otherwise, it returns 1 (in case strings are not matched). See the example below to see how CompareTo method works.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
using System;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str1, str2;
str1 = “This is String tutorial”;
str2 = “This is String tutorial”;
if (str1.CompareTo(str2)==0)
{
Console.WriteLine(“Strings are matched!”);
}
else
{
Console.WriteLine(“Strings are not matched!”);
}
Console.ReadLine();
}
}
}
|
You can see, as both strings are matched so CompareTo method returned 0.
The Equals method
The C# Equals method of strings also compares two strings. The specified string is matched with a given string and equals C# method returns true or false if the strings are matched or not.
See the following example of using the equals method.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
using System;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str1, str2;
str1 = “This is String tutorial”;
str2 = “This is String tutorial”;
if (str1.Equals(str2) == true)
{
Console.WriteLine(“Strings are matched by using c# Equals!”);
}
else
{
Console.WriteLine(“Strings are not matched!”);
}
Console.ReadLine();
}
}
}
|
C# clone method example
The clone method of the string makes the clone of a string. See the example of using the clone method.
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 csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str1;
str1 = “This is String tutorial”;
Console.WriteLine(str1.Clone());
Console.ReadLine();
}
}
}
|
C# contains method example
The contains method checks if the given string value exists in specified string or not. The contains method returns the Boolean value, true or false. The returned value is true if the match is found otherwise false will be returned.
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
28
29
30
31
32
33
34
35
36
37
38
39
|
using System;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str;
str = “This is String tutorial”;
if (str.Contains(“tutorial”) == true)
{
Console.WriteLine(“The ‘tutorial’ exists!”);
}
else {
Console.WriteLine(“The ‘tutorial’ does not exist!”);
}
Console.ReadLine();
}
}
}
|
As you can see, we checked whether the word “tutorial” exists or not in the specified string. As it exists, the if condition evaluated as true after using the string contains method.
The GetHashCode method example
The C# GetHashCode method returns the hashcode of the given string. Following example shows using the GetHashCode method.
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 csharp_demo
{
class Program
{
static void Main(string[] args)
{
string str;
str = “This is String tutorial”;
Console.WriteLine(str.GetHashCode());
Console.ReadLine();
}
}
}
|
The join method example
The C# join method joins all elements of the array by a given separator. For example, a comma or space etc.
The following example adds a space between each array element of the string. Later, we added a comma separator by using the join method.
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
28
29
30
31
32
33
34
35
36
37
|
using System;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
String[] strarr = new String[4];
strarr[0] = “This”;
strarr[1] = “is”;
strarr[2] = “String”;
strarr[3] = “Tutorial”;
string str = String.Join(” “,strarr);
Console.WriteLine(str);
Console.WriteLine(String.Join(“,”, strarr));
Console.ReadLine();
}
}
}
|
Leave A Comment?