Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String.Split stuff #39850

Merged
merged 8 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions docs/csharp/how-to/parse-strings-using-split.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Divide strings using String.Split (C# Guide)"
description: The Split method returns an array of strings split from a set of delimiters. It's an easy way to extract substrings from a string.
ms.date: 01/03/2018
ms.date: 03/06/2024
helpviewer_keywords:
- "splitting strings [C#]"
- "Split method [C#]"
Expand All @@ -12,34 +12,27 @@ ms.custom: mvc
---
# How to separate strings using String.Split in C\#

The <xref:System.String.Split%2A?displayProperty=nameWithType> method creates an
array of substrings by splitting the input string based on one or more delimiters. This method is often the easiest way to separate a string on word boundaries. It's also used
to split strings on other specific characters or strings.
The <xref:System.String.Split%2A?displayProperty=nameWithType> method creates an array of substrings by splitting the input string based on one or more delimiters. This method is often the easiest way to separate a string on word boundaries. It's also used to split strings on other specific characters or strings.

[!INCLUDE[interactive-note](~/includes/csharp-interactive-note.md)]

The following code splits a common phrase into an array of strings for each word.

:::code language="csharp" interactive="try-dotnet-method" source="../../../samples/snippets/csharp/how-to/strings/ParseStringsUsingSplit.cs" id="Snippet1":::

Every instance of a separator character produces a value in the
returned array. Consecutive separator characters produce the empty string
as a value in the returned array. You can see how an empty string is created in the following example,
which uses the space character as a separator.
Every instance of a separator character produces a value in the returned array. Since arrays in C# are zero-indexed, each string in the array is indexed from 0 to the value returned by the <xref:System.Array.Length%2A?displayProperty=nameWithType> property minus 1:

:::code language="csharp" interactive="try-dotnet-method" source="../../../samples/snippets/csharp/how-to/strings/ParseStringsUsingSplit.cs" id="Snippet1.5":::

Consecutive separator characters produce the empty string as a value in the returned array. You can see how an empty string is created in the following example, which uses the space character as a separator.

:::code language="csharp" interactive="try-dotnet-method" source="../../../samples/snippets/csharp/how-to/strings/ParseStringsUsingSplit.cs" id="Snippet2":::

This behavior makes it easier for formats like comma-separated values (CSV)
files representing tabular data. Consecutive commas represent a blank column.
This behavior makes it easier for formats like comma-separated values (CSV) files representing tabular data. Consecutive commas represent a blank column.

You can pass an optional <xref:System.StringSplitOptions.RemoveEmptyEntries?displayProperty=nameWithType> parameter to
exclude any empty strings in the returned array. For more complicated processing of the returned
collection, you can use [LINQ](/dotnet/csharp/linq/) to manipulate
the result sequence.
You can pass an optional <xref:System.StringSplitOptions.RemoveEmptyEntries?displayProperty=nameWithType> parameter to exclude any empty strings in the returned array. For more complicated processing of the returned collection, you can use [LINQ](/dotnet/csharp/linq/) to manipulate the result sequence.

<xref:System.String.Split%2A?displayProperty=nameWithType> can use multiple separator characters.
The following example uses spaces, commas, periods, colons, and tabs as separating characters, which are passed to <xref:System.String.Split%2A> in an array.
The loop at the bottom of the code displays each of the words in the returned array.
<xref:System.String.Split%2A?displayProperty=nameWithType> can use multiple separator characters. The following example uses spaces, commas, periods, colons, and tabs as separating characters, which are passed to <xref:System.String.Split%2A> in an array. The loop at the bottom of the code displays each of the words in the returned array.

:::code language="csharp" interactive="try-dotnet-method" source="../../../samples/snippets/csharp/how-to/strings/ParseStringsUsingSplit.cs" id="Snippet3":::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,33 @@ public static class ParseStringsUsingSplit
{
public static void Examples()
{
Console.WriteLine("Example one");
Console.WriteLine("Split words");
Console.WriteLine();
SplitWords();

Console.WriteLine("Example two");
Console.WriteLine("Enumerate words");
Console.WriteLine();
EnumerateWords();

Console.WriteLine("Split words with repeated separators");
Console.WriteLine();
SplitWordsWithRepeatedSeparators();

Console.WriteLine("Example three");
Console.WriteLine("Split on multiple chars");
Console.WriteLine();
SplitOnMultipleChars();

Console.WriteLine("Example four");
Console.WriteLine("Split on multiple chars with gaps");
Console.WriteLine();
SplitOnMultipleCharsWithGaps();

Console.WriteLine("Example five");
Console.WriteLine("Split using strings");
Console.WriteLine();
SplitUsingStrings();

Console.WriteLine("Split into no more than four substrings");
Console.WriteLine();
SplitFourTimes();
}

private static void SplitWords()
Expand All @@ -41,6 +49,19 @@ private static void SplitWords()
// </Snippet1>
}

private static void EnumerateWords()
{
// <Snippet1.5>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ');

for (int i = 0; i < words.Length; i++)
{
System.Console.WriteLine($"Index {i}: <{words[i]}>");
}
// </Snippet1.5>
}

private static void SplitWordsWithRepeatedSeparators()
{
// <Snippet2>
Expand Down Expand Up @@ -107,5 +128,18 @@ private static void SplitUsingStrings()
}
// </Snippet5>
}

private static void SplitFourTimes()
{
// <Snippet6>
string phrase = "The quick brown fox jumps over the lazy dog.";
string[] words = phrase.Split(' ', 4, System.StringSplitOptions.None);

foreach (var word in words)
{
System.Console.WriteLine($"<{word}>");
}
// </Snippet6>
}
}
}
Loading