Substring Extraction & Indexing
When working with strings in C#, you often need to extract specific parts of a larger text—such as pulling out a first name from a full name, or finding a particular word in a sentence. To do this, you must understand zero-based indexing, which means that the first character in a string is at position 0, the second at position 1, and so on. Extracting substrings involves specifying the position (index) where you want to start, and sometimes how many characters to include.
Program.cs
12345678910111213141516171819using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string fullName = "John Doe"; int spaceIndex = fullName.IndexOf(' '); string firstName = fullName.Substring(0, spaceIndex); string lastName = fullName.Substring(spaceIndex + 1); Console.WriteLine("First name: " + firstName); Console.WriteLine("Last name: " + lastName); } } }
In this example, you see how IndexOf is used to find the position of the first space character in the string, which separates the first and last names. The Substring method then extracts the first name by starting at index 0 and taking all characters up to the space, and the last name by starting right after the space and taking the rest of the string. IndexOf returns the index of the first occurrence of the specified character, while Substring can extract text starting from a given index, and optionally for a specified length.
Program.cs
1234567891011121314151617181920212223242526using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string input = "SingleName"; int spaceIndex = input.IndexOf(' '); if (spaceIndex != -1) { string firstPart = input.Substring(0, spaceIndex); string secondPart = input.Substring(spaceIndex + 1); Console.WriteLine("First part: " + firstPart); Console.WriteLine("Second part: " + secondPart); } else { Console.WriteLine("No space found in input."); } } } }
It is important to handle cases where the character you are searching for is not present in the string. IndexOf returns -1 if the character cannot be found, so you should always check for this before using the result with Substring. This prevents errors and allows you to handle edge cases, such as when a delimiter is missing, in a user-friendly way.
Definition: Zero-based indexing means that counting starts from 0. In string manipulation, the first character is at index 0, the second at index 1, and so on. This is essential for correctly specifying positions when extracting or searching within strings.
123string text = "Example"; int position = text.IndexOf('a'); // Finds the index of 'a' string part = text.Substring(2, 3); // Extracts 3 characters starting at index 2
1. What does IndexOf(' ') return for the string "John Doe"?
2. Which method extracts a portion of a string?
3. If IndexOf returns -1, what does it mean?
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
Substring Extraction & Indexing
Deslize para mostrar o menu
When working with strings in C#, you often need to extract specific parts of a larger text—such as pulling out a first name from a full name, or finding a particular word in a sentence. To do this, you must understand zero-based indexing, which means that the first character in a string is at position 0, the second at position 1, and so on. Extracting substrings involves specifying the position (index) where you want to start, and sometimes how many characters to include.
Program.cs
12345678910111213141516171819using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string fullName = "John Doe"; int spaceIndex = fullName.IndexOf(' '); string firstName = fullName.Substring(0, spaceIndex); string lastName = fullName.Substring(spaceIndex + 1); Console.WriteLine("First name: " + firstName); Console.WriteLine("Last name: " + lastName); } } }
In this example, you see how IndexOf is used to find the position of the first space character in the string, which separates the first and last names. The Substring method then extracts the first name by starting at index 0 and taking all characters up to the space, and the last name by starting right after the space and taking the rest of the string. IndexOf returns the index of the first occurrence of the specified character, while Substring can extract text starting from a given index, and optionally for a specified length.
Program.cs
1234567891011121314151617181920212223242526using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { string input = "SingleName"; int spaceIndex = input.IndexOf(' '); if (spaceIndex != -1) { string firstPart = input.Substring(0, spaceIndex); string secondPart = input.Substring(spaceIndex + 1); Console.WriteLine("First part: " + firstPart); Console.WriteLine("Second part: " + secondPart); } else { Console.WriteLine("No space found in input."); } } } }
It is important to handle cases where the character you are searching for is not present in the string. IndexOf returns -1 if the character cannot be found, so you should always check for this before using the result with Substring. This prevents errors and allows you to handle edge cases, such as when a delimiter is missing, in a user-friendly way.
Definition: Zero-based indexing means that counting starts from 0. In string manipulation, the first character is at index 0, the second at index 1, and so on. This is essential for correctly specifying positions when extracting or searching within strings.
123string text = "Example"; int position = text.IndexOf('a'); // Finds the index of 'a' string part = text.Substring(2, 3); // Extracts 3 characters starting at index 2
1. What does IndexOf(' ') return for the string "John Doe"?
2. Which method extracts a portion of a string?
3. If IndexOf returns -1, what does it mean?
Obrigado pelo seu feedback!