Skip to content

Commit 7ad8358

Browse files
committed
final edit pass
1 parent fc51f21 commit 7ad8358

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

docs/csharp/how-to/concatenate-multiple-strings.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "How to concatenate multiple strings"
33
description: There are multiple ways to concatenate strings in C#. Learn the options and the reasons behind different choices.
4-
ms.date: 1/31/2025
4+
ms.date: 02/18/2025
55
helpviewer_keywords:
66
- "joining strings [C#]"
77
- "concatenating strings [C#]"
@@ -43,11 +43,11 @@ You can use string interpolation to initialize a constant string when all the ex
4343

4444
## `String.Format`
4545

46-
Another method to concatenate strings is <xref:System.String.Format%2A?displayProperty=nameWithType>. This method works well when you're building a string from a small number of component strings.
46+
Another method to concatenate strings is <xref:System.String.Format%2A?displayProperty=nameWithType>. This method works well when you're building a string from a few component strings.
4747

4848
## `StringBuilder`
4949

50-
In other cases, you might be combining strings in a loop where you don't know how many source strings you're combining, and the actual number of source strings can be large. The <xref:System.Text.StringBuilder> class was designed for these scenarios. The following code uses the <xref:System.Text.StringBuilder.Append%2A> method of the <xref:System.Text.StringBuilder> class to concatenate strings.
50+
In other cases, you might be combining strings in a loop where the actual number of source strings can be large. The <xref:System.Text.StringBuilder> class was designed for these scenarios. The following code uses the <xref:System.Text.StringBuilder.Append%2A> method of the <xref:System.Text.StringBuilder> class to concatenate strings.
5151

5252
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/strings/Concatenate.cs" id="Snippet4":::
5353

docs/csharp/how-to/modify-string-contents.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "How to modify string contents"
33
description: Review examples of several techniques to modify existing string contents in C#, which return a new string object.
4-
ms.date: 02/26/2018
4+
ms.date: 02/18/2025
55
helpviewer_keywords:
66
- "strings [C#], modifying"
77
---
@@ -19,23 +19,23 @@ The following code creates a new string by replacing existing text with a substi
1919

2020
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/strings/ModifyStrings.cs" id="Snippet1":::
2121

22-
The preceding code demonstrates this *immutable* property of strings. You can see in the preceding example that the original string, `source`, is not modified. The <xref:System.String.Replace%2A?displayProperty=nameWithType> method creates a new `string` containing the modifications.
22+
The preceding code demonstrates this *immutable* property of strings. You can see in the preceding example that the original string, `source`, isn't modified. The <xref:System.String.Replace%2A?displayProperty=nameWithType> method creates a new `string` containing the modifications.
2323

24-
The <xref:System.String.Replace%2A> method can replace either strings or single characters. In both cases, every occurrence of the sought text is replaced. The following example replaces all ' ' characters with '\_':
24+
The <xref:System.String.Replace%2A> method can replace either strings or single characters. In both cases, every occurrence of the sought text is replaced. The following example replaces all ' ' characters with '\_':
2525

2626
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/strings/ModifyStrings.cs" id="Snippet2":::
2727

2828
The source string is unchanged, and a new string is returned with the replacement.
2929

3030
## Trim white space
3131

32-
You can use the <xref:System.String.Trim%2A?displayProperty=nameWithType>, <xref:System.String.TrimStart%2A?displayProperty=nameWithType>, and <xref:System.String.TrimEnd%2A?displayProperty=nameWithType> methods to remove any leading or trailing white space. The following code shows an example of each. The source string does not change; these methods return a new string with the modified contents.
32+
You can use the <xref:System.String.Trim%2A?displayProperty=nameWithType>, <xref:System.String.TrimStart%2A?displayProperty=nameWithType>, and <xref:System.String.TrimEnd%2A?displayProperty=nameWithType> methods to remove any leading or trailing white space. The following code shows an example of each. The source string doesn't change; these methods return a new string with the modified contents.
3333

3434
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/strings/ModifyStrings.cs" id="Snippet3":::
3535

3636
## Remove text
3737

38-
You can remove text from a string using the <xref:System.String.Remove%2A?displayProperty=nameWithType> method. This method removes a number of characters starting at a specific index. The following example shows how to use <xref:System.String.IndexOf%2A?displayProperty=nameWithType> followed by <xref:System.String.Remove%2A> to remove text from a string:
38+
You can remove text from a string using the <xref:System.String.Remove%2A?displayProperty=nameWithType> method. This method removes the specified number of characters starting at a specific index. The following example shows how to use <xref:System.String.IndexOf%2A?displayProperty=nameWithType> followed by <xref:System.String.Remove%2A> to remove text from a string:
3939

4040
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/strings/ModifyStrings.cs" id="Snippet4":::
4141

@@ -59,11 +59,11 @@ The following example shows how to replace a set of characters in a string. Firs
5959

6060
## Programmatically build up string content
6161

62-
Since strings are immutable, the previous examples all create temporary strings or character arrays. In high-performance scenarios, it may be desirable to avoid these heap allocations. .NET Core provides a <xref:System.String.Create%2A?displayProperty=nameWithType> method that allows you to programmatically fill in the character content of a string via a callback while avoiding the intermediate temporary string allocations.
62+
Since strings are immutable, the previous examples all create temporary strings or character arrays. In high-performance scenarios, it's desirable to avoid these heap allocations. .NET Core provides a <xref:System.String.Create%2A?displayProperty=nameWithType> method that allows you to programmatically fill in the character content of a string via a callback while avoiding the intermediate temporary string allocations.
6363

6464
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/strings/ModifyStrings.cs" id="Snippet7":::
6565

66-
You could modify a string in a fixed block with unsafe code, but it is **strongly** discouraged to modify the string content after a string is created. Doing so will break things in unpredictable ways. For example, if someone interns a string that has the same content as yours, they'll get your copy and won't expect that you are modifying their string.
66+
You could modify a string in a fixed block with unsafe code, but it's **strongly** discouraged to modify the string content after a string is created. Doing so causes unpredictable bugs. For example, if someone interns a string that has the same content as yours, they get your copy and didn't expect that you're modifying their string.
6767

6868
## See also
6969

docs/csharp/how-to/search-strings.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
---
22
title: "How to search strings"
33
description: Learn about two strategies to search for text in strings in C#. String class methods search for specific text. Regular expressions search for patterns in text.
4-
ms.date: 02/21/2018
4+
ms.date: 02/18/2025
55
helpviewer_keywords:
66
- "searching strings [C#]"
77
- "strings [C#], searching with String methods"
88
- "strings [C#], searching with regular expressions"
9-
ms.assetid: fb1d9a6d-598d-4a35-bd5f-b86012edcb2b
109
---
1110

1211
# How to search strings
@@ -15,7 +14,7 @@ You can use two main strategies to search for text in strings. Methods of the <x
1514

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

18-
The [string](../language-reference/builtin-types/reference-types.md#the-string-type) type, which is an alias for the <xref:System.String?displayProperty=nameWithType> class, provides a number of useful methods for searching the contents of a string. Among them are <xref:System.String.Contains%2A>, <xref:System.String.StartsWith%2A>, <xref:System.String.EndsWith%2A>, <xref:System.String.IndexOf%2A>, <xref:System.String.LastIndexOf%2A>. The <xref:System.Text.RegularExpressions.Regex?displayProperty=nameWithType> class provides a rich vocabulary to search for patterns in text. In this article, you learn these techniques and how to choose the best method for your needs.
17+
The [string](../language-reference/builtin-types/reference-types.md#the-string-type) type, which is an alias for the <xref:System.String?displayProperty=nameWithType> class, provides many useful methods for searching the contents of a string. Among them are <xref:System.String.Contains%2A>, <xref:System.String.StartsWith%2A>, <xref:System.String.EndsWith%2A>, <xref:System.String.IndexOf%2A>, <xref:System.String.LastIndexOf%2A>. The <xref:System.Text.RegularExpressions.Regex?displayProperty=nameWithType> class provides a rich vocabulary to search for patterns in text. In this article, you learn these techniques and how to choose the best method for your needs.
1918

2019
## Does a string contain text?
2120

@@ -37,7 +36,7 @@ The <xref:System.Text.RegularExpressions.Regex?displayProperty=nameWithType> cla
3736

3837
The following code example searches for the word "the" or "their" in a sentence, ignoring case. The static method <xref:System.Text.RegularExpressions.Regex.IsMatch%2A?displayProperty=nameWithType> performs the search. You give it the string to search and a search pattern. In this case, a third argument specifies case-insensitive search. For more information, see <xref:System.Text.RegularExpressions.RegexOptions?displayProperty=nameWithType>.
3938

40-
The search pattern describes the text you search for. The following table describes each element of the search pattern. (The table below uses the single `\`, which must be escaped as `\\` in a C# string).
39+
The search pattern describes the text you search for. The following table describes each element of the search pattern. (The following table uses the single `\`, which must be escaped as `\\` in a C# string).
4140

4241
| Pattern | Meaning |
4342
|----------|----------------------------------|
@@ -48,18 +47,18 @@ The search pattern describes the text you search for. The following table descri
4847
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/strings/SearchStrings.cs" id="Snippet3":::
4948

5049
> [!TIP]
51-
> The `string` methods are usually better choices when you are searching for an exact string. Regular expressions are better when you are searching for some pattern in a source string.
50+
> The `string` methods are generally better choices when you're searching for an exact string. Regular expressions are better when you're searching for some pattern in a source string.
5251
5352
## Does a string follow a pattern?
5453

55-
The following code uses regular expressions to validate the format of each string in an array. The validation requires that each string have the form of a telephone number in which three groups of digits are separated by dashes, the first two groups contain three digits, and the third group contains four digits. The search pattern uses the regular expression `^\\d{3}-\\d{3}-\\d{4}$`. For more information, see [Regular Expression Language - Quick Reference](../../standard/base-types/regular-expression-language-quick-reference.md).
54+
The following code uses regular expressions to validate the format of each string in an array. The validation requires that each string has the form of a telephone number: three groups of digits separated by dashes where the first two groups contain three digits and the third group contains four digits. The search pattern uses the regular expression `^\\d{3}-\\d{3}-\\d{4}$`. For more information, see [Regular Expression Language - Quick Reference](../../standard/base-types/regular-expression-language-quick-reference.md).
5655

5756
| Pattern | Meaning |
5857
|---------|-------------------------------------|
5958
| `^` | matches the beginning of the string |
60-
| `\d{3}` | matches exactly 3 digit characters |
59+
| `\d{3}` | matches exactly three digit characters |
6160
| `-` | matches the '-' character |
62-
| `\d{4}` | matches exactly 4 digit characters |
61+
| `\d{4}` | matches exactly four digit characters |
6362
| `$` | matches the end of the string |
6463

6564
:::code language="csharp" interactive="try-dotnet-method" source="./snippets/\strings/SearchStrings.cs" id="Snippet4":::

0 commit comments

Comments
 (0)