Strings are a programmatic representation of text. For various operations it is useful to think of strings as character arrays as they are usually implemented as.
Creating Strings
Creating a string is simple. Have a look at this code:
string s = "test";
What text we want the string to hold is put in double quotes and assigned to a variable using the assignment operator. Simple. It is important to note that once a string is created it is immutable. If you want to change the string you have to make a new string. We will later look at how to modify strings without creating a new string.
We will now consider various string functions.
String Length
It is common to want to know how long a string is. For this we simply use the Length method of the string class.
Example:
string s = "test"; Console.WriteLine(s.Length());The output of the above code will be:
Quote
4
IndexOf
The IndexOf method returns the index of the first instance of a character in a string or -1 if it does not exist.
Example:
string s = "test";
Console.WriteLine(s.IndexOf('t'));
The output of this code will be:Quote
0
What if we want to know the last index of a character? For this we make use of the LastIndexOf method as follows:
string s = "test";
Console.WriteLine(s.LastIndexOf('t'));
The output is:
Quote
3
If we want the first occurrence after the first occurrence we would write:
string s = "test";
Console.WriteLine(s.IndexOf('t',1));
This code starts the search for t at index 1 in s.
I’m going to leave a challenge here: how would you find all instances of t in s using the IndexOf method?
See if you can find out how to do the same thing using LastIndexOf.
Replace
This method replaces all instances of one character with another character.
Note: it doesn’t actually replace the character in the original string but produces a new string.
Example:
string s = "test";
s = s.Replace('t', 'r');
Console.WriteLine(s);
The output of this code is:
Quote
resr
A second version of this method exists which lets you replace all occurrences of a substring with another substring. Example:
string s = "test";
s = s.Replace("es", "on");
Console.WriteLine(s);
s = s.Replace("t", "x");
Console.WriteLine(s);
The first replace takes the string “test” and replaces all occurrences of “es” with “on” producing the string “tont”. The second replace takes the string “tont” and replaces all occurrences of “t” with “x” producing the string “xonx”. In the execution of this program, s is never changed but three new strings are created. If you are going to be changing characters often it is best not to use replace but to use StringBuilders which we will look at soon.
Substring
One of the most useful string method is the substring method. This lets you extract a portion of the string into a new string. To use this method you need to know the start index of where you want to start copying from and optionally the number of characters you want to copy. Like arrays, strings start at index 0 so we need to keep that in mind when using this method.
Examples:
string s = "welcome to codecall"; Console.WriteLine(s.Substring(1));
This example copies the entire string from index 1 to the last character in the string. The output is then:
Quote
elcome to codecall
If we only want to copy a portion of the string we use a second implementation of this method.
Example:
string s = "welcome to codecall"; Console.WriteLine(s.Substring(2,5));
This code produces the string starting at index 2. The second parameter tells us how many characters to take. In this case we are going to copy 5 characters.
The output is:
Quote
lcome
StartsWith
This method simply takes a string and returns true if the object this method is being called on starts with that string. Example:
string s = "test";
Console.WriteLine(s.StartsWith("t"));
Output is:
Quote
True
EndsWith
This method simply takes a string and returns true if the object this method is being called on ends with that string. Example:
string s = "test";
Console.WriteLine(s.EndsWith("ast"));
Output is:
Quote
False


Sign In
Create Account


Back to top









