Introduction 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
123456789101112131415161718using 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
123456789101112131415161718using 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.
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.
12345string first = "Good"; string second = "Morning"; string combined = first + " " + second; string phrase = "Today is " + "Monday"; string message = $"Welcome, {second}!";
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?
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Incrível!
Completion taxa melhorada para 4.76
Introduction 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
123456789101112131415161718using 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
123456789101112131415161718using 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.
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.
12345string first = "Good"; string second = "Morning"; string combined = first + " " + second; string phrase = "Today is " + "Monday"; string message = $"Welcome, {second}!";
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?
Obrigado pelo seu feedback!