Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Substring Extraction & Indexing | Fundamentals of Strings
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Strings & Text Processing

bookSubstring 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

Program.cs

copy
12345678910111213141516171819
using 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

Program.cs

copy
1234567891011121314151617181920212223242526
using 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.

Note
Definition

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.

123
string 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
copy

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?

question mark

What does IndexOf(' ') return for the string "John Doe"?

Select the correct answer

question mark

Which method extracts a portion of a string?

Select the correct answer

question mark

If IndexOf returns -1, what does it mean?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain how to handle cases where the character isn't found in the string?

What happens if I use Substring with an invalid index?

Can you give more examples of extracting parts of a string in C#?

bookSubstring Extraction & Indexing

Stryg for at vise menuen

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

Program.cs

copy
12345678910111213141516171819
using 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

Program.cs

copy
1234567891011121314151617181920212223242526
using 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.

Note
Definition

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.

123
string 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
copy

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?

question mark

What does IndexOf(' ') return for the string "John Doe"?

Select the correct answer

question mark

Which method extracts a portion of a string?

Select the correct answer

question mark

If IndexOf returns -1, what does it mean?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
some-alt