Skip to content

Commit 5ce0bb4

Browse files
authored
Add GitHub Copilot usage scenario
1 parent 8370c43 commit 5ce0bb4

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

docs/csharp/how-to/parse-strings-using-split.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ The <xref:System.String.Split%2A?displayProperty=nameWithType> method creates an
1717

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

20+
## String.Split examples
21+
2022
The following code splits a common phrase into an array of strings for each word.
2123

2224
:::code language="csharp" interactive="try-dotnet-method" source="../../../samples/snippets/csharp/how-to/strings/ParseStringsUsingSplit.cs" id="Snippet1":::
@@ -45,6 +47,65 @@ Consecutive instances of any separator produce the empty string in the output ar
4547

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

50+
## Use GitHub Copilot to split a string using String.Split in C\#
51+
52+
You can use GitHub Copilot in your IDE to generate code to split strings using `String.Split` in C#.
53+
54+
If you're using [Visual Studio 2022 version 17.8 or later](/visualstudio/releases/2022/release-notes), you can try the AI-driven [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states) to generate code to split an input string by one or more delimiters into substrings. Submit your question as a prompt in the Copilot chat window, as in the following example. You can also submit prompts using [inline chat](/visualstudio/ide/visual-studio-github-copilot-chat#ask-questions-in-the-inline-chat-view) in the editor window itself.
55+
56+
> [!NOTE]
57+
> GitHub Copilot is powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions. For more information about the general use of GitHub Copilot, product impact, human oversight, and privacy, see [GitHub Copilot FAQs](https://github.com/features/copilot#faq).
58+
59+
The following text shows an example prompt for Copilot Chat:
60+
61+
> Generate C# code to split a string. Input string is "You win some. You lose some.". Delimiters are space and period. Provide example output.
62+
63+
You can customize the prompt to use strings and delimiters per your requirements.
64+
65+
You can use chat features, such as [slash commands, references to files, methods, or classes, and threads](/visualstudio/ide/copilot-chat-context), to set intent and get better answers with scoped context. For an existing class in a project that's open in the IDE, you can prompt GitHub Copilot with `/generate code to convert the string "123" to a number`.
66+
67+
The following output shows an example Copilot Chat response:
68+
69+
```output
70+
Here is a C# code example to split the string "You win some. You lose some." using space and period as delimiters:
71+
72+
using System;
73+
74+
class Program
75+
{
76+
static void Main()
77+
{
78+
string input = "You win some. You lose some.";
79+
char[] delimiters = new char[] { ' ', '.' };
80+
string[] parts = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
81+
82+
foreach (string part in parts)
83+
{
84+
Console.WriteLine(part);
85+
}
86+
}
87+
}
88+
89+
**Example Output**
90+
You
91+
win
92+
some
93+
You
94+
lose
95+
some
96+
```
97+
98+
When Copilot returns a code block, the response includes options to copy the code, insert the code into a new file, or preview the code output.
99+
100+
> [!NOTE]
101+
> Your results might be different from what's shown in the example responses. AI models are non-deterministic, which means that they can return different responses when asked the same question. This might be due to additional learning and adaption over time, language variation, changes in context, such as your chat history, and more.
102+
103+
For more information, see:
104+
105+
* [GitHub Copilot Trust Center](https://resources.github.com/copilot-trust-center/)
106+
* [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states)
107+
* [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview)
108+
48109
## See also
49110

50111
- [Extract elements from a string](../../standard/base-types/divide-up-strings.md)

0 commit comments

Comments
 (0)