One of the most used elements, if not the one we show the most to the user when programming, are text strings, commonly called phrases. Therefore, in this video we are going to see how to work with them.
Table of Contents
1 - The char type
The first thing we will look at is the character type, called char
, which is a single character.
It has several characteristics:
- They are predefined data of type char and take up 1 byte in memory.
- To indicate a character we must wrap it in single quotes. If we use two, it will be considered a string.
char caracter = 'k';
That's how you define a character, but under normal circumstances they're rarely used. What we do use is the string type.
2 - The string type
What we use a lot are text strings, which are several char elements one after the other, forming phrases. This is called type string
.
There are several ways to create a string. The first is direct and the simplest:
- Direct assignment.
string test = "NetMentor";
- The second is using a char array. We'll see what arrays are in the next post.
char[] name = { 'N', 'E', 'T'};string stringName = new string(name);
- The third is concatenating two variables
var nombreConcatenado = name1 + name2
- Finally, we can use string interpolation, which we can see in more detail in another video, but for now, we'll just note that it allows us to put variables inside as long as we enclose them in curly braces.
var stringConVariables = $"el nombre es {variable}.";
2.1 - Formatting a string
Usually, when we want to display data, it has a type, whether it's a percentage, currency, a specific date format, or many other options, since it's much better to display €50 than just 50, or use the percent sign when talking about percentages.
To do this, we use a method within the string class called format, and we use it as follows:
string.Format("La temperatura actual es {0}°C.", 20.4);
As the first parameter, we pass the phrase we want to print. Inside the phrase, we can include variables, as we explained earlier. To do this, we insert the variable number {n} in brackets, since as the second parameter (and subsequent ones) we include the variables we want to show.
2.2 - String class functions
Finally, within string we'll see that by default, string provides many commonly used functions to make developers' lives easier. These are:
Let's imagine we have a string like the following:
string myString = "Tengo un vaso lleno de";
- If we want to convert it to uppercase:
string mayusculas = myString.ToUpper();
- If we want to convert it to lowercase
string minusculas = myString.ToLower();
- If we want to compare one phrase to another, it will return true or false
bool sonIguales = myString.Equals("Tengo un vaso lleno de");
- If we want to know the position of a character
int posicion = myString.IndexOf("o");
- If we want to concatenate two strings
string frase = myString + “ zumo de naranja”;
- If we want to know whether a phrase contains another phrase, it returns true or false
bool contiene = myString.Contains(“vaso”);
3 - StringBuilder
When we create a string variable, strings are immutable, which means that once a value is assigned, it cannot be changed. If we want to update a string, what the code does internally is create a new memory space, which can cause performance issues.
To avoid this problem, or if we need a mutable text string, we have the StringBuilder
type.
StringBuilder sb = new StringBuilder();
In this case, if we want to add text to the variable, we use the following method:
sb.Append(“texto”);
This way, it uses the same memory space and doesn't create duplicates.
To print it, we just need to call the .ToString()
method on StringBuilder
Console.Writeline(sb.ToString())
In real life, we will commonly use concatenation to add text, since the few bytes it uses don't justify the logic of creating the object. But, if we're going to build a long text, such as joining several XML files, it may be worth it to use a StringBuilder to use less memory space.
If there is any problem you can add a comment bellow or contact me in the website's contact form