Formatação Básica de Strings
We can output text and a variable value using an already known method (the +
operator):
main.cs
12345678910111213using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int number = 10; Console.WriteLine("The value is: " + number); } } }
However there are better and more suitable methods for string formatting which can also handle complex cases. The first method is by using the placeholder syntax. To understand this syntax we will use the Console.WriteLine
method:
main.cs
123456789101112131415using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int val1 = 10; int val2 = 20; int val3 = 30; Console.WriteLine("The values are: {0}, {1} and {2}", val1, val2, val3); } } }
In the above code we insert placeholders in the string where values are to be inserted. The syntax of a placeholder is a number (index) enclosed in curly brackets {index}
and the value index starts from 0
. When the output string is generated, the placeholders are filled with the corresponding values that are passed as additional arguments to the Console.WriteLine method.
Existe outro método muito mais simples de formatar strings. Podemos usar o caractere para indicar que uma string é uma string formatada e inserir diretamente valores dentro dela usando as chaves (
{}`):
Usando esta sintaxe, também podemos gerar e armazenar strings formatadas em variáveis de string:
Using this syntax, we can also generate and store formatted strings into string variables:
main.cs
12345678910111213141516using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int val1 = 10; int val2 = 20; int val3 = 30; string text = $"The values are: {val1}, {val2} and {val3}"; Console.WriteLine(text); } } }
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 1.59
Formatação Básica de Strings
Deslize para mostrar o menu
We can output text and a variable value using an already known method (the +
operator):
main.cs
12345678910111213using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int number = 10; Console.WriteLine("The value is: " + number); } } }
However there are better and more suitable methods for string formatting which can also handle complex cases. The first method is by using the placeholder syntax. To understand this syntax we will use the Console.WriteLine
method:
main.cs
123456789101112131415using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int val1 = 10; int val2 = 20; int val3 = 30; Console.WriteLine("The values are: {0}, {1} and {2}", val1, val2, val3); } } }
In the above code we insert placeholders in the string where values are to be inserted. The syntax of a placeholder is a number (index) enclosed in curly brackets {index}
and the value index starts from 0
. When the output string is generated, the placeholders are filled with the corresponding values that are passed as additional arguments to the Console.WriteLine method.
Existe outro método muito mais simples de formatar strings. Podemos usar o caractere para indicar que uma string é uma string formatada e inserir diretamente valores dentro dela usando as chaves (
{}`):
Usando esta sintaxe, também podemos gerar e armazenar strings formatadas em variáveis de string:
Using this syntax, we can also generate and store formatted strings into string variables:
main.cs
12345678910111213141516using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int val1 = 10; int val2 = 20; int val3 = 30; string text = $"The values are: {val1}, {val2} and {val3}"; Console.WriteLine(text); } } }
Obrigado pelo seu feedback!