Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Introduction to Strings | Fundamentals of Strings
C# Strings & Text Processing

bookIntroduction to Strings

Strings are a fundamental part of programming, representing sequences of characters such as words, sentences, or any text data. In C#, a string is a built-in data type used for handling text, making it essential for tasks like displaying messages, reading user input, or processing files. You encounter strings constantly in everyday applications, from typing a message to searching for information online. Understanding how to work with strings in C# lays the foundation for effective text processing and user interaction in your programs.

Program.cs

Program.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { public class Program { public static void Main() { string greeting = "Hello"; string name = "Alice"; string fullGreeting = greeting + " " + name; Console.WriteLine(fullGreeting); int length = fullGreeting.Length; Console.WriteLine("The greeting has " + length + " characters."); } } }

In this example, you see how to create strings using string literals—text enclosed in double quotes. The variables greeting and name are both strings. You combine, or concatenate, these strings with the + operator, adding a space between them to create fullGreeting. When you print fullGreeting, the output is the two words combined with a space. The Length property of a string gives you the number of characters in the string, including spaces and punctuation. Here, fullGreeting.Length returns the total number of characters in the combined greeting.

Program.cs

Program.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { public class Program { public static void Main() { string name = "Bob"; string interpolated = $"Hello, {name}! Welcome to C#."; Console.WriteLine(interpolated); string withEscapes = "Line 1\nLine 2\tTabbed"; Console.WriteLine(withEscapes); } } }

String interpolation lets you insert variable values directly into a string using the $ symbol and curly braces, as in $"Hello, {name}!". This makes it easier to build complex strings without multiple concatenations. Escape sequences are special character combinations that start with a backslash, like \n for a newline (moving to the next line) and \t for a tab (adding horizontal spacing). These are useful for formatting output, such as creating multi-line messages or aligning text in columns.

Note
Definition

In C#, strings are immutable, meaning once a string is created, its contents cannot be changed. Any operation that appears to modify a string actually creates a new string. This design improves performance and security, but it also means you need to be mindful when performing many modifications to strings.

12345
string first = "Good"; string second = "Morning"; string combined = first + " " + second; string phrase = "Today is " + "Monday"; string message = $"Welcome, {second}!";
copy

1. What is the result of concatenating "Hello" + " " + "World" in C#?

2. Which property gives the number of characters in a string?

3. What does the escape sequence '\n' represent in a string?

question mark

What is the result of concatenating "Hello" + " " + "World" in C#?

Select the correct answer

question mark

Which property gives the number of characters in a string?

Select the correct answer

question mark

What does the escape sequence '\n' represent in a string?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookIntroduction to Strings

Deslize para mostrar o menu

Strings are a fundamental part of programming, representing sequences of characters such as words, sentences, or any text data. In C#, a string is a built-in data type used for handling text, making it essential for tasks like displaying messages, reading user input, or processing files. You encounter strings constantly in everyday applications, from typing a message to searching for information online. Understanding how to work with strings in C# lays the foundation for effective text processing and user interaction in your programs.

Program.cs

Program.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { public class Program { public static void Main() { string greeting = "Hello"; string name = "Alice"; string fullGreeting = greeting + " " + name; Console.WriteLine(fullGreeting); int length = fullGreeting.Length; Console.WriteLine("The greeting has " + length + " characters."); } } }

In this example, you see how to create strings using string literals—text enclosed in double quotes. The variables greeting and name are both strings. You combine, or concatenate, these strings with the + operator, adding a space between them to create fullGreeting. When you print fullGreeting, the output is the two words combined with a space. The Length property of a string gives you the number of characters in the string, including spaces and punctuation. Here, fullGreeting.Length returns the total number of characters in the combined greeting.

Program.cs

Program.cs

copy
123456789101112131415161718
using System; namespace ConsoleApp { public class Program { public static void Main() { string name = "Bob"; string interpolated = $"Hello, {name}! Welcome to C#."; Console.WriteLine(interpolated); string withEscapes = "Line 1\nLine 2\tTabbed"; Console.WriteLine(withEscapes); } } }

String interpolation lets you insert variable values directly into a string using the $ symbol and curly braces, as in $"Hello, {name}!". This makes it easier to build complex strings without multiple concatenations. Escape sequences are special character combinations that start with a backslash, like \n for a newline (moving to the next line) and \t for a tab (adding horizontal spacing). These are useful for formatting output, such as creating multi-line messages or aligning text in columns.

Note
Definition

In C#, strings are immutable, meaning once a string is created, its contents cannot be changed. Any operation that appears to modify a string actually creates a new string. This design improves performance and security, but it also means you need to be mindful when performing many modifications to strings.

12345
string first = "Good"; string second = "Morning"; string combined = first + " " + second; string phrase = "Today is " + "Monday"; string message = $"Welcome, {second}!";
copy

1. What is the result of concatenating "Hello" + " " + "World" in C#?

2. Which property gives the number of characters in a string?

3. What does the escape sequence '\n' represent in a string?

question mark

What is the result of concatenating "Hello" + " " + "World" in C#?

Select the correct answer

question mark

Which property gives the number of characters in a string?

Select the correct answer

question mark

What does the escape sequence '\n' represent in a string?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 1
some-alt