Skip to content

Commit 92e109f

Browse files
anandmegBillWagner
andauthored
Add copilot use case examples (#43241)
* Add copilot use case example * Update how-to-convert-a-string-to-a-number.md * Add metadata * Add GitHub Copilot usage scenario * Update parse-strings-using-split.md * Update example * update ms.date * Refine string splitting example in documentation * Updates * Add images * Add image * Fix image file path * Add image * Update docs/csharp/how-to/parse-strings-using-split.md Co-authored-by: Bill Wagner <[email protected]> * Create media folder * Move image file * Delete readme * Create media sub folder * Delete docs/csharp/programming-guide/types/media/how-to-convert-a-string-to-a-number * Create media subfolder * Move image file * Delete readme * Delete docs/media/github-copilot-chat-convert-string-to-number.png * Delete docs/media/github-copilot-chat-string-split.png * Fix image * fix image file path * Fix error * Fix error * Fix image source path in documentation --------- Co-authored-by: Bill Wagner <[email protected]>
1 parent 0eae6c9 commit 92e109f

File tree

4 files changed

+130
-3
lines changed

4 files changed

+130
-3
lines changed
185 KB
Loading

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
---
22
title: "Divide strings using String.Split"
33
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.
4-
ms.date: 03/06/2024
4+
ms.date: 10/31/2024
55
helpviewer_keywords:
66
- "splitting strings [C#]"
77
- "Split method [C#]"
88
- "strings [C#], splitting"
99
- "parse strings"
1010
ms.assetid: 729c2923-4169-41c6-9c90-ef176c1e2953
11-
ms.custom: mvc
11+
ms.custom: mvc, vs-copilot-horizontal
12+
ms.collection: ce-skilling-ai-copilot
1213
---
1314
# How to separate strings using String.Split in C\#
1415

1516
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.
1617

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

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

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

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

50+
## Use GitHub Copilot to split a string
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 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.
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 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.
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 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`.
66+
67+
The following output shows an example Copilot Chat response:
68+
69+
```output
70+
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:
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[] substrings = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
81+
82+
foreach (string substring in substrings)
83+
{
84+
Console.WriteLine(substring);
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+
:::image type="content" source="./media/parse-strings-using-split/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/parse-strings-using-split/github-copilot-chat-string-split.png":::
104+
105+
For more information, see:
106+
107+
* [GitHub Copilot Trust Center](https://resources.github.com/copilot-trust-center/)
108+
* [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states)
109+
* [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview)
110+
47111
## See also
48112

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

docs/csharp/programming-guide/types/how-to-convert-a-string-to-a-number.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
---
22
title: "How to convert a string to a number"
33
description: Learn how to convert a string to a number in C# by calling the Parse, TryParse, or Convert class methods.
4-
ms.date: 03/15/2024
4+
ms.date: 10/31/2024
55
helpviewer_keywords:
66
- "conversions [C#]"
77
- "conversions [C#], string to int"
88
- "converting strings to int [C#]"
99
- "strings [C#], converting to int"
1010
ms.topic: how-to
11+
ms.collection: ce-skilling-ai-copilot
12+
ms.custom: vs-copilot-horizontal
1113
ms.assetid: 467b9979-86ee-4afd-b734-30299cda91e3
1214
adobe-target: true
1315
---
@@ -50,3 +52,64 @@ The following table lists some of the methods from the <xref:System.Convert> cla
5052
The following example calls the <xref:System.Convert.ToInt32%28System.String%29?displayProperty=nameWithType> method to convert an input string to an [int](../../language-reference/builtin-types/integral-numeric-types.md). The example catches the two most common exceptions thrown by this method: <xref:System.FormatException> and <xref:System.OverflowException>. If the resulting number can be incremented without exceeding <xref:System.Int32.MaxValue?displayProperty=nameWithType>, the example adds 1 to the result and displays the output.
5153

5254
[!code-csharp[Parsing with Convert methods](~/samples/snippets/csharp/programming-guide/string-to-number/convert/program.cs)]
55+
56+
## Use GitHub Copilot to convert a string to a number
57+
58+
You can use GitHub Copilot in your IDE to generate code to convert a string to a number in C#.
59+
60+
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 convert a string to a number. 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.
61+
62+
> [!NOTE]
63+
> 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).
64+
65+
The following text shows an example prompt for Copilot Chat:
66+
67+
> Generate C# code to convert the string "123" to a number using TryParse. Provide example output.
68+
69+
You can customize the prompt to use a string per your requirements.
70+
71+
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 convert the string string1 in #Filename to a number`.
72+
73+
The following output shows an example Copilot Chat response:
74+
75+
```output
76+
Here is a C# code snippet to convert the string "123" to a number using TryParse:
77+
78+
using System;
79+
80+
class Program
81+
{
82+
static void Main()
83+
{
84+
string numberString = "123";
85+
int number;
86+
bool success = int.TryParse(numberString, out number);
87+
88+
if (success)
89+
{
90+
// Example output
91+
Console.WriteLine("The converted number is: " + number);
92+
}
93+
else
94+
{
95+
Console.WriteLine("Conversion failed.");
96+
}
97+
}
98+
}
99+
100+
When you run this code, the output will be:
101+
The converted number is: 123
102+
```
103+
104+
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.
105+
106+
> [!NOTE]
107+
> 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.
108+
109+
:::image type="content" source="./media/how-to-convert-a-string-to-a-number/github-copilot-chat-convert-string-to-number.png" alt-text="Screenshot that shows using GitHub Copilot Chat in Visual Studio to convert a string to a number." lightbox="./media/how-to-convert-a-string-to-a-number/github-copilot-chat-convert-string-to-number.png":::
110+
111+
For more information, see:
112+
113+
* [GitHub Copilot Trust Center](https://resources.github.com/copilot-trust-center/)
114+
* [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states)
115+
* [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview)
180 KB
Loading

0 commit comments

Comments
 (0)