String Manipulation
We are going to see some useful methods that can be used on String
objects.
To illustrate the methods, we can use this variable as our example:
var text = "It's a cold day in the woods";
Substring
This method returns a brand-new string with the value: "a cold day"
var subtext = text.Substring(5, 10);
Console.WriteLine(subtext);
So, we can get only a part of a string
.
Join
We can create a new string based on an array of values and a separator.
var numbers = new[] { 1, 2, 3, 4, 5 };
var result = string.Join(",", numbers);
Console.WriteLine(result);
Concat
We can combine two string together. If we run just this line:
string.Concat(text, " of the village");
Console.WriteLine(text);
The value of the text variable doesn't change (we know that the string is immutable). Therefore, we need to reassign the new value:
text = string.Concat(text, " of the village");
Console.WriteLine(text);
Instead of string.Concat()
, we can use the plus operator +
to combine strings together.
text += " of the village";
Console.WriteLine(text);
This operator is called the concatenation operator.
+ operator
The compiler is smart enough to tell that when you use the +
operator with strings, you mean that you want it to combine them togheter. With integers, it performs addition.
The compiler is dope.
Replace
We can replace any string occurance inside of a string:
text = text.Replace("cold", "warm");
Console.WriteLine(text);
This works the same as the string.Concat()
method: it returns a new string. We need to reassign the value to the text
variable. If not, the value of text
remains the same.
Like the Substring
method, Replace
actually returns a new string and does not modify the original string.
ToUpper
We can transform all the characters of a string to upper case:
text = text.ToUpper();
Console.WriteLine(text);
ToLower
text = text.ToLower();
Console.WriteLine(text);
Insert
We can insert another sequence inside of a string:
text = text.Insert(12, "foggy ");
Console.WriteLine(text);
IndexOf
We can find the starting index of a specific string inside another one:
var index = text.IndexOf("warm");
Console.WriteLine(index);
Negative index
If the method returns a negative index (-1), then it means that there is no such string inside.
Length
We can get the length / size of a string through the Length property:
var length = text.Length;
Console.WriteLine(length);
Contains
We can use the Contains
method to check if there is a string in another one:
if (text.Contains("warm"))
{
Console.WriteLine("The string was found");
}
Copy
We can copy the value of one string to another one:
var anotherText = string.Copy(text);
Console.WriteLine(anotherText);
StartsWith
This method checks whether or not a string starts with another one:
var result = text.StartsWith("It's a");
Console.Writeline(result); // True
EndsWith
This performs in the opposite way of StartsWith
:
var result = text.StartsWith("in the woods");
Console.Writeline(result); // True
Trim
We can remove white-spaces from the beginning and end of a string with the Trim
method:
var phoneNumber = " 232 323 232 ";
phoneNumber = phoneNumber.Trim();
WARNING
Notice that this doesn't affect the white-spaces inside of the string.
You can change how the Trim
method works by providing a set of characters:
var phoneNumber = ".232 323 232,";
var chars = new char[] { '.', ',' };
phoneNumber = phoneNumber.Trim(chars);
ToArray
We can transform a string to an array:
var textAsArray = text.ToArray();
Console.WriteLine(textAsArray);
Example
Let's now use these methods to not guess where a string is, but to do it programmatically:
var term = "warm";
var position = text.IndexOf(term);
if (position > -1)
{
text = text.Insert(position + term.Length, " sunny");
}
Console.WriteLine(text);
if (text.Contains("foggy"))
text = text.Replace("foggy", string.Empty);
To make the code cleaner, we can assign the value to a variable:
term = "foggy";
if (text.Contains(term))
text = text.Replace(term, string.Empty);
Remember
Keep in mind that all the methods that appear/seem to change a string, in fact they return a new one. This can have huge perfomance issues which we will look at in a bit.
Bye
You can have fun with these methods 😅