Skip to content

Commit 8fb4a43

Browse files
anandmeggewarren
andauthored
Updates per revised guidance on adding a copilot section to docs (#43994)
* Update parse-strings-using-split.md per revised guidance on adding a copilot section to docs * Update how-to-convert-a-string-to-a-number.md * Delete docs/csharp/programming-guide/types/media/how-to-convert-a-string-to-a-number directory * Delete docs/csharp/how-to/media/parse-strings-using-split directory * Update docs/csharp/how-to/parse-strings-using-split.md Co-authored-by: Genevieve Warren <[email protected]> * Fix example. * Remove extra newlines * Modify prompt example to use Bill's suggested prompt --------- Co-authored-by: Genevieve Warren <[email protected]>
1 parent ddd0d3c commit 8fb4a43

File tree

4 files changed

+19
-105
lines changed

4 files changed

+19
-105
lines changed

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

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

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

20+
> [!TIP]
21+
> You can use AI assistance to [split a string with GitHub Copilot](#use-github-copilot-to-split-a-string).
22+
2023
## String.Split examples
2124

2225
The following code splits a common phrase into an array of strings for each word.
@@ -49,64 +52,19 @@ Consecutive instances of any separator produce the empty string in the output ar
4952

5053
## Use GitHub Copilot to split a string
5154

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).
55+
You can use GitHub Copilot in your IDE to generate code to split strings using `String.Split` in C#. You can customize the prompt to use strings and delimiters per your requirements.
5856

5957
The following text shows an example prompt for Copilot Chat:
6058

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
59+
```copilot-prompt
60+
Generate C# code to use Split.String to split a string into substrings.
61+
Input string is "You win some. You lose some." Delimiters are space and period.
62+
Provide example output.
9663
```
9764

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:
65+
GitHub Copilot is powered by AI, so surprises and mistakes are possible. For more information, see [Copilot FAQs](https://aka.ms/copilot-general-use-faqs).
10666

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)
67+
Learn more about [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states) and [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview).
11068

11169
## See also
11270

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

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ You convert a `string` to a number by calling the `Parse` or `TryParse` method f
2020
It's slightly more efficient and straightforward to call a `TryParse` method (for example, [`int.TryParse("11", out number)`](xref:System.Int32.TryParse%2A)) or `Parse` method (for example, [`var number = int.Parse("11")`](xref:System.Int32.Parse%2A)). Using a <xref:System.Convert> method is more useful for general objects that implement <xref:System.IConvertible>.
2121

2222
You use `Parse` or `TryParse` methods on the numeric type you expect the string contains, such as the <xref:System.Int32?displayProperty=nameWithType> type. The <xref:System.Convert.ToInt32%2A?displayProperty=nameWithType> method uses <xref:System.Int32.Parse%2A> internally. The `Parse` method returns the converted number; the `TryParse` method returns a boolean value that indicates whether the conversion succeeded, and returns the converted number in an `out` parameter. If the string isn't in a valid format, `Parse` throws an exception, but `TryParse` returns `false`. When calling a `Parse` method, you should always use exception handling to catch a <xref:System.FormatException> when the parse operation fails.
23-
23+
24+
> [!TIP]
25+
> You can use AI assistance to [convert a string to a number with GitHub Copilot](#use-github-copilot-to-convert-a-string-to-a-number).
26+
2427
## Call Parse or TryParse methods
2528

2629
The `Parse` and `TryParse` methods ignore white space at the beginning and at the end of the string, but all other characters must be characters that form the appropriate numeric type (`int`, `long`, `ulong`, `float`, `decimal`, and so on). Any white space within the string that forms the number causes an error. For example, you can use `decimal.TryParse` to parse "10", "10.3", or " 10 ", but you can't use this method to parse 10 from "10X", "1 0" (note the embedded space), "10 .3" (note the embedded space), "10e1" (`float.TryParse` works here), and so on. A string whose value is `null` or <xref:System.String.Empty?displayProperty=nameWithType> fails to parse successfully. You can check for a null or empty string before attempting to parse it by calling the <xref:System.String.IsNullOrEmpty%2A?displayProperty=nameWithType> method.
@@ -55,61 +58,14 @@ The following example calls the <xref:System.Convert.ToInt32%28System.String%29?
5558

5659
## Use GitHub Copilot to convert a string to a number
5760

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).
61+
You can use GitHub Copilot in your IDE to generate C# code to convert a string to a number. You can customize the prompt to use a string per your requirements.
6462

6563
The following text shows an example prompt for Copilot Chat:
6664

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
65+
```copilot-prompt
66+
Show me how to parse a string as a number, but don't throw an exception if the input string doesn't represent a number.
10267
```
10368

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:
69+
GitHub Copilot is powered by AI, so surprises and mistakes are possible. For more information, see [Copilot FAQs](https://aka.ms/copilot-general-use-faqs).
11270

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)
71+
Learn more about [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states) and [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview).

0 commit comments

Comments
 (0)