One of the most commonly used elements, if not the most frequently exposed to the user when we program, are text strings, commonly called sentences. Therefore, in this video, we will see how to work with them.
Index
1 - The char type
The first thing we will see is the character type, called char
, which represents a single character.
It has several characteristics:
- They are predefined char type data and take up 1 byte in memory.
- To indicate a character, we must enclose it in single quotes. If we use two, it will be considered a string.
char caracter = 'k';
That is how you define a character, but under normal circumstances, they are hardly ever used. What we do use is the string type.
2 - The string type
What we use a lot are text strings, that is, several char elements in a row, forming sentences. This is called the string
type.
There are several ways to create a string. The first is direct assignment and is the simplest:
- Assignment in a straightforward way.
string test = "NetMentor";
- The second is using a char array. We will see what arrays are in the next post.
char[] name = { 'N', 'E', 'T'};
string stringName = new string(name);
- The third is by concatenating two variables
var nombreConcatenado = name1 + name2
- Finally, we can use string interpolation, which we will see in more detail in another video. For now, we can say that it allows us to have variables inside as long as we enclose them in curly braces.
var stringConVariables = $"el nombre es {variable}.";
2.1 - Formatting a string
Normally, when we want to display data, it is usually of a certain type, be it percentage, money, a specific date format, or many other options, as it is much better to show €50 than just 50, or the percentage symbol when talking about percentages.
For that, we use a method that is inside 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 sentence we want to print. Inside the sentence, we can include variables, as we have indicated in the previous section. For this, we insert the variable number {n} inside brackets, since as the second (and next) parameter(s), we include the variables we want to display.
2.2 - Functions of the string class
Finally, within string, we will see that by default, string comes with many commonly used functions to make developers' lives easier, such as:
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 sentence with 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 if a sentence contains a certain phrase, it returns true or false
bool contiene = myString.Contains(“vaso”);
3 - StringBuilder
When we create a string variable, it is immutable, meaning that once the value is assigned, it cannot be changed. If we want to update a string, the code internally creates 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”);
which does use the same memory spaces and does not create duplications.
To print it, we simply have to call the .ToString()
method inside StringBuilder
.
Console.Writeline(sb.ToString())
In real life, we commonly use concatenation to add text, as the few bytes it consumes are not worth the logic of creating the object. But if what we want to do is build a long text, such as joining several XMLs, then it may be worth using a stringbuilder to consume less memory.
If there is any problem you can add a comment bellow or contact me in the website's contact form