-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add copilot use case examples #43241
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
d178e2d
Add copilot use case example
anandmeg 34a635f
Update how-to-convert-a-string-to-a-number.md
anandmeg 8370c43
Add metadata
anandmeg 5ce0bb4
Add GitHub Copilot usage scenario
anandmeg cf63877
Update parse-strings-using-split.md
anandmeg dc25a9d
Update example
anandmeg ac8aa72
update ms.date
anandmeg 6ac187d
Refine string splitting example in documentation
anandmeg 6dad3d2
Updates
anandmeg eb6f9c4
Add images
anandmeg ac330c1
Add image
anandmeg 6ccfd76
Fix image file path
anandmeg ed85b4c
Add image
anandmeg 068ce40
Update docs/csharp/how-to/parse-strings-using-split.md
anandmeg 1f5961c
Create media folder
anandmeg 3024f0d
Move image file
anandmeg f271bbb
Delete readme
anandmeg 68eea42
Create media sub folder
anandmeg c60cc7d
Delete docs/csharp/programming-guide/types/media/how-to-convert-a-str…
anandmeg 1eecb46
Create media subfolder
anandmeg e268b42
Move image file
anandmeg 6893dbf
Delete readme
anandmeg c9640e9
Delete docs/media/github-copilot-chat-convert-string-to-number.png
anandmeg a58ae75
Delete docs/media/github-copilot-chat-string-split.png
anandmeg c6bd8d9
Fix image
anandmeg 533e013
fix image file path
anandmeg fb785fb
Fix error
anandmeg 232efbb
Fix error
anandmeg e15b0e3
Fix image source path in documentation
anandmeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,24 @@ | ||
| --- | ||
| title: "Divide strings using String.Split" | ||
| 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: 03/06/2024 | ||
| ms.date: 10/31/2024 | ||
| helpviewer_keywords: | ||
| - "splitting strings [C#]" | ||
| - "Split method [C#]" | ||
| - "strings [C#], splitting" | ||
| - "parse strings" | ||
| ms.assetid: 729c2923-4169-41c6-9c90-ef176c1e2953 | ||
| ms.custom: mvc | ||
| ms.custom: mvc, vs-copilot-horizontal | ||
| ms.collection: ce-skilling-ai-copilot | ||
| --- | ||
| # 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. | ||
|
|
||
| [!INCLUDE[interactive-note](~/includes/csharp-interactive-note.md)] | ||
|
|
||
| ## String.Split examples | ||
|
Check failure on line 20 in docs/csharp/how-to/parse-strings-using-split.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"::: | ||
|
|
@@ -44,6 +47,67 @@ | |
|
|
||
| :::code language="csharp" interactive="try-dotnet-method" source="../../../samples/snippets/csharp/how-to/strings/ParseStringsUsingSplit.cs" id="Snippet5"::: | ||
|
|
||
| ## Use GitHub Copilot to split a string | ||
|
|
||
| You can use GitHub Copilot in your IDE to generate code to split strings using `String.Split` in C#. | ||
|
|
||
| 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 into substrings based on one or more delimiters. 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. | ||
|
|
||
| > [!NOTE] | ||
| > 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). | ||
| The following text shows an example prompt for Copilot Chat: | ||
|
|
||
| > Generate C# code to use Split.String split a string into substrings. Input string is "You win some. You lose some." Delimiters are space and period. Provide example output. | ||
| You can customize the prompt to use strings and delimiters per your requirements. | ||
|
|
||
| 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 file that's open in the IDE, you can prompt GitHub Copilot using inline chat with `/generate code to split string1 in #Filename by delimiter spaces`. | ||
|
|
||
| The following output shows an example Copilot Chat response: | ||
|
|
||
| ```output | ||
| Here is an example of how you can use the Split method in C# to split the string "You win some. You lose some." using space and period as delimiters: | ||
| using System; | ||
| class Program | ||
| { | ||
| static void Main() | ||
| { | ||
| string input = "You win some. You lose some."; | ||
| char[] delimiters = new char[] { ' ', '.' }; | ||
| string[] substrings = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); | ||
| foreach (string substring in substrings) | ||
| { | ||
| Console.WriteLine(substring); | ||
| } | ||
| } | ||
| } | ||
| **Example Output** | ||
| You | ||
| win | ||
| some | ||
| You | ||
| lose | ||
| some | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| > [!NOTE] | ||
| > 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. | ||
| :::image type="content" source="../../media/github-copilot-chat-string-split.png" alt-text="Screenshot that shows using GitHub Copilot Chat in Visual Studio to split a string into substrings." lightbox="../../media/github-copilot-chat-string-split.png"::: | ||
anandmeg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| For more information, see: | ||
|
|
||
| * [GitHub Copilot Trust Center](https://resources.github.com/copilot-trust-center/) | ||
| * [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states) | ||
| * [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview) | ||
|
|
||
| ## See also | ||
|
|
||
| - [Extract elements from a string](../../standard/base-types/divide-up-strings.md) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.