diff --git a/.openpublishing.redirection.standard.json b/.openpublishing.redirection.standard.json index 44291283c667d..6bbdea81b7725 100644 --- a/.openpublishing.redirection.standard.json +++ b/.openpublishing.redirection.standard.json @@ -58,6 +58,10 @@ "source_path_from_root": "/docs/standard/base-types/backtracking.md", "redirect_url": "/dotnet/standard/base-types/backtracking-in-regular-expressions" }, + { + "source_path_from_root": "/docs/standard/base-types/basic-string-operations.md", + "redirect_url": "/dotnet/standard/base-types/creating-new" + }, { "source_path_from_root": "/docs/standard/base-types/best-practices.md", "redirect_url": "/dotnet/standard/base-types/best-practices-regex" diff --git a/docs/csharp/language-reference/builtin-types/reference-types.md b/docs/csharp/language-reference/builtin-types/reference-types.md index 89297c0c79f65..f13a34454034b 100644 --- a/docs/csharp/language-reference/builtin-types/reference-types.md +++ b/docs/csharp/language-reference/builtin-types/reference-types.md @@ -2,7 +2,7 @@ title: "Built-in reference types" description: "Learn about reference types that have C# keywords you can use to declare them." ms.date: 10/07/2025 -f1_keywords: +f1_keywords: - "object_CSharpKeyword" - "object" - "delegate_CSharpKeyword" @@ -11,7 +11,7 @@ f1_keywords: - "string" - "string_CSharpKeyword" - "Utf8StringLiteral_CSharpKeyword" -helpviewer_keywords: +helpviewer_keywords: - "object keyword [C#]" - "delegate keyword [C#]" - "function pointers [C#]" @@ -221,7 +221,7 @@ Delegate combination or removal fails with a runtime exception when the delegate ```csharp Action stringAction = str => {}; Action objectAction = obj => {}; - + // Valid due to implicit reference conversion of // objectAction to Action, but might fail // at run time. @@ -233,7 +233,7 @@ You can create a delegate with the correct runtime type by creating a new delega ```csharp Action stringAction = str => {}; Action objectAction = obj => {}; - + // Creates a new delegate instance with a runtime type of Action. Action wrappedObjectAction = new Action(objectAction); @@ -290,7 +290,6 @@ For more information, see the following sections of the [C# language specificati - [Events](../../programming-guide/events/index.md) - [Using Type dynamic](../../advanced-topics/interop/using-type-dynamic.md) - [Best Practices for Using Strings](../../../standard/base-types/best-practices-strings.md) -- [Basic String Operations](../../../standard/base-types/basic-string-operations.md) - [Creating New Strings](../../../standard/base-types/creating-new.md) - [Type-testing and cast operators](../operators/type-testing-and-cast.md) - [How to safely cast using pattern matching and the as and is operators](../../fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) diff --git a/docs/csharp/programming-guide/strings/index.md b/docs/csharp/programming-guide/strings/index.md index 330559415cd1f..05cda626f60b1 100644 --- a/docs/csharp/programming-guide/strings/index.md +++ b/docs/csharp/programming-guide/strings/index.md @@ -188,7 +188,7 @@ Because the type implements and methods to perform basic string operations. +- [Using the StringBuilder class in .NET](../../../standard/base-types/stringbuilder.md) - [Parsing Strings](../../../standard/base-types/parsing-strings.md): Describes how to convert string representations of .NET base types to instances of the corresponding types. - [Parsing Date and Time Strings in .NET](../../../standard/base-types/parsing-datetime.md): Shows how to convert a string such as "01/24/2008" to a object. - [Comparing Strings](../../../standard/base-types/comparing.md): Includes information about how to compare strings and provides examples in C# and Visual Basic. diff --git a/docs/fundamentals/toc.yml b/docs/fundamentals/toc.yml index 5f6cb1b04327a..af0aab3c22482 100644 --- a/docs/fundamentals/toc.yml +++ b/docs/fundamentals/toc.yml @@ -617,10 +617,8 @@ items: href: ../standard/base-types/string-comparison-net-5-plus.md - name: Basic string operations items: - - name: Overview - href: ../standard/base-types/basic-string-operations.md - displayName: string operations - name: Create new strings + displayName: string operations href: ../standard/base-types/creating-new.md - name: Trim and remove characters href: ../standard/base-types/trimming.md diff --git a/docs/standard/base-types/basic-manipulations.md b/docs/standard/base-types/basic-manipulations.md index 4e6ed8f84a33a..32264e5b2d077 100644 --- a/docs/standard/base-types/basic-manipulations.md +++ b/docs/standard/base-types/basic-manipulations.md @@ -7,11 +7,10 @@ dev_langs: - "vb" helpviewer_keywords: - "strings [.NET], examples" -ms.assetid: 121d1eae-251b-44c0-8818-57da04b8215e --- # How to: Perform Basic String Manipulations in .NET -The following example uses some of the methods discussed in the [Basic String Operations](basic-string-operations.md) topics to construct a class that performs string manipulations in a manner that might be found in a real-world application. The `MailToData` class stores the name and address of an individual in separate properties and provides a way to combine the `City`, `State`, and `Zip` fields into a single string for display to the user. Furthermore, the class allows the user to enter the city, state, and zip code information as a single string. The application automatically parses the single string and enters the proper information into the corresponding property. +The following example constructs a class that performs string manipulations in a manner that might be found in a real-world application. The `MailToData` class stores the name and address of an individual in separate properties and provides a way to combine the `City`, `State`, and `Zip` fields into a single string for display to the user. Furthermore, the class allows the user to enter the city, state, and zip code information as a single string. The application automatically parses the single string and enters the proper information into the corresponding property. For simplicity, this example uses a console application with a command-line interface. @@ -21,7 +20,3 @@ For simplicity, this example uses a console application with a command-line inte [!code-vb[Conceptual.String.BasicOps#1](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.string.basicops/vb/basicops.vb#1)] When the preceding code is executed, the user is asked to enter their name and address. The application places the information in the appropriate properties and displays the information back to the user, creating a single string that displays the city, state, and zip code information. - -## See also - -- [Basic String Operations](basic-string-operations.md) diff --git a/docs/standard/base-types/basic-string-operations.md b/docs/standard/base-types/basic-string-operations.md deleted file mode 100644 index bd6159783a20f..0000000000000 --- a/docs/standard/base-types/basic-string-operations.md +++ /dev/null @@ -1,24 +0,0 @@ ---- -title: Basic String Operations in .NET -description: Learn about the basic operations that you can perform on strings. -ms.date: "03/30/2017" -ms.topic: article -helpviewer_keywords: - - "strings [.NET], basic string operations" - - "custom strings" -ms.assetid: 8133d357-90b5-4b62-9927-43323d99b6b6 -ms.custom: seadec18 ---- -# Basic string operations in .NET - -Applications often respond to users by constructing messages based on user input. For example, it is not uncommon for websites to respond to a newly logged-on user with a specialized greeting that includes the user's name. - -Several methods in the and classes allow you to dynamically construct custom strings to display in your user interface. These methods also help you perform a number of basic string operations like creating new strings from arrays of bytes, comparing the values of strings, and modifying existing strings. - -## Related sections - -[Type Conversion in .NET](type-conversion.md)\ -Describes how to convert one type into another type. - -[Formatting Types](formatting-types.md)\ -Describes how to format strings using format specifiers. diff --git a/docs/standard/base-types/changing-case.md b/docs/standard/base-types/changing-case.md index 109bf81b946c8..4f4f6d0ec1e58 100644 --- a/docs/standard/base-types/changing-case.md +++ b/docs/standard/base-types/changing-case.md @@ -69,5 +69,4 @@ Note that although it is culture-sensitive, the method to find th ## See also -- [Basic string operations in .NET](basic-string-operations.md) - [.NET regular expressions](regular-expressions.md) - [How to parse strings using String.Split in C#](../../csharp/how-to/parse-strings-using-split.md) diff --git a/docs/standard/base-types/padding.md b/docs/standard/base-types/padding.md index 8d2b88941495b..87846918e7366 100644 --- a/docs/standard/base-types/padding.md +++ b/docs/standard/base-types/padding.md @@ -38,7 +38,3 @@ Use one of the following methods to create a new string tha [!code-csharp[Conceptual.String.BasicOps#4](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.string.basicops/cs/padding.cs#4)] [!code-vb[Conceptual.String.BasicOps#4](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.string.basicops/vb/padding.vb#4)] - -## See also - -- [Basic String Operations](basic-string-operations.md) diff --git a/docs/standard/base-types/stringbuilder.md b/docs/standard/base-types/stringbuilder.md index 889f9e59f9a92..2a2e5598d91df 100644 --- a/docs/standard/base-types/stringbuilder.md +++ b/docs/standard/base-types/stringbuilder.md @@ -97,5 +97,4 @@ The object is immutable. Every time you use one of the meth ## See also - -- [Basic String Operations](basic-string-operations.md) - [Formatting Types](formatting-types.md) diff --git a/docs/standard/base-types/trimming.md b/docs/standard/base-types/trimming.md index df8c187405df3..7449ff09cb94c 100644 --- a/docs/standard/base-types/trimming.md +++ b/docs/standard/base-types/trimming.md @@ -77,7 +77,3 @@ If you're parsing a sentence into individual words, you might end up with words [!code-csharp[Conceptual.String.BasicOps#23](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.string.basicops/cs/replace1.cs#23)] [!code-vb[Conceptual.String.BasicOps#23](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.string.basicops/vb/replace1.vb#23)] - -## See also - -- [Basic String Operations](basic-string-operations.md) diff --git a/docs/visual-basic/programming-guide/language-features/strings/string-basics.md b/docs/visual-basic/programming-guide/language-features/strings/string-basics.md index b2c8bf2c3a35e..d7e29871e22a2 100644 --- a/docs/visual-basic/programming-guide/language-features/strings/string-basics.md +++ b/docs/visual-basic/programming-guide/language-features/strings/string-basics.md @@ -2,7 +2,7 @@ description: "Learn more about: String Basics in Visual Basic" title: "String Basics" ms.date: 07/20/2015 -helpviewer_keywords: +helpviewer_keywords: - "strings [Visual Basic], Like operator" - "strings [Visual Basic], Visual Basic" - "strings [Visual Basic], regular expressions" @@ -10,69 +10,68 @@ ms.assetid: 5674418d-f00d-4f72-9f98-d15897793350 --- # String Basics in Visual Basic -The `String` data type represents a series of characters (each representing in turn an instance of the `Char` data type). This topic introduces the basic concepts of strings in Visual Basic. - -## String Variables - - An instance of a string can be assigned a literal value that represents a series of characters. For example: - - [!code-vb[VbVbalrStrings#63](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#63)] - - A `String` variable can also accept any expression that evaluates to a string. Examples are shown below: - - [!code-vb[VbVbalrStrings#64](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#64)] - - Any literal that is assigned to a `String` variable must be enclosed in quotation marks (""). This means that a quotation mark within a string cannot be represented by a quotation mark. For example, the following code causes a compiler error: - - [!code-vb[VbVbalrStrings#65](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#65)] - - This code causes an error because the compiler terminates the string after the second quotation mark, and the remainder of the string is interpreted as code. To solve this problem, Visual Basic interprets two quotation marks in a string literal as one quotation mark in the string. The following example demonstrates the correct way to include a quotation mark in a string: - - [!code-vb[VbVbalrStrings#66](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#66)] - - In the preceding example, the two quotation marks preceding the word `Look` become one quotation mark in the string. The three quotation marks at the end of the line represent one quotation mark in the string and the string termination character. - - String literals can contain multiple lines: - -```vb -Dim x = "hello -world" -``` - - The resulting string contains newline sequences that you used in your string literal (vbcr, vbcrlf, etc.). You no longer need to use the old workaround: - -```vb -Dim x = .Value -``` - -## Characters in Strings - - A string can be thought of as a series of `Char` values, and the `String` type has built-in functions that allow you to perform many manipulations on a string that resemble the manipulations allowed by arrays. Like all array in .NET Framework, these are zero-based arrays. You may refer to a specific character in a string through the `Chars` property, which provides a way to access a character by the position in which it appears in the string. For example: - - [!code-vb[VbVbalrStrings#67](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#67)] - - In the above example, the `Chars` property of the string returns the fourth character in the string, which is `D`, and assigns it to `myChar`. You can also get the length of a particular string through the `Length` property. If you need to perform multiple array-type manipulations on a string, you can convert it to an array of `Char` instances using the `ToCharArray` function of the string. For example: - - [!code-vb[VbVbalrStrings#68](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#68)] - - The variable `myArray` now contains an array of `Char` values, each representing a character from `myString`. - -## The Immutability of Strings - - A string is *immutable*, which means its value cannot be changed once it has been created. However, this does not prevent you from assigning more than one value to a string variable. Consider the following example: - - [!code-vb[VbVbalrStrings#69](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#69)] - - Here, a string variable is created, given a value, and then its value is changed. - - More specifically, in the first line, an instance of type `String` is created and given the value `This string is immutable`. In the second line of the example, a new instance is created and given the value `Or is it?`, and the string variable discards its reference to the first instance and stores a reference to the new instance. - - Unlike other intrinsic data types, `String` is a reference type. When a variable of reference type is passed as an argument to a function or subroutine, a reference to the memory address where the data is stored is passed instead of the actual value of the string. So in the previous example, the name of the variable remains the same, but it points to a new and different instance of the `String` class, which holds the new value. - +The `String` data type represents a series of characters. Each character represents an instance of the `Char` data type. This topic introduces the basic concepts of strings in Visual Basic. + +## String Variables + + An instance of a string can be assigned a literal value that represents a series of characters. For example: + + [!code-vb[VbVbalrStrings#63](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#63)] + + A `String` variable can also accept any expression that evaluates to a string. Examples are shown below: + + [!code-vb[VbVbalrStrings#64](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#64)] + + Any literal that is assigned to a `String` variable must be enclosed in quotation marks (""). This means that a quotation mark within a string cannot be represented by a quotation mark. For example, the following code causes a compiler error: + + [!code-vb[VbVbalrStrings#65](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#65)] + + This code causes an error because the compiler terminates the string after the second quotation mark, and the remainder of the string is interpreted as code. To solve this problem, Visual Basic interprets two quotation marks in a string literal as one quotation mark in the string. The following example demonstrates the correct way to include a quotation mark in a string: + + [!code-vb[VbVbalrStrings#66](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#66)] + + In the preceding example, the two quotation marks preceding the word `Look` become one quotation mark in the string. The three quotation marks at the end of the line represent one quotation mark in the string and the string termination character. + + String literals can contain multiple lines: + +```vb +Dim x = "hello +world" +``` + + The resulting string contains newline sequences that you used in your string literal (vbcr, vbcrlf, etc.). You no longer need to use the old workaround: + +```vb +Dim x = .Value +``` + +## Characters in Strings + + A string can be thought of as a series of `Char` values, and the `String` type has built-in functions that allow you to perform many manipulations on a string that resemble the manipulations allowed by arrays. Like all array in .NET Framework, these are zero-based arrays. You may refer to a specific character in a string through the `Chars` property, which provides a way to access a character by the position in which it appears in the string. For example: + + [!code-vb[VbVbalrStrings#67](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#67)] + + In the above example, the `Chars` property of the string returns the fourth character in the string, which is `D`, and assigns it to `myChar`. You can also get the length of a particular string through the `Length` property. If you need to perform multiple array-type manipulations on a string, you can convert it to an array of `Char` instances using the `ToCharArray` function of the string. For example: + + [!code-vb[VbVbalrStrings#68](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#68)] + + The variable `myArray` now contains an array of `Char` values, each representing a character from `myString`. + +## The Immutability of Strings + + A string is *immutable*, which means its value cannot be changed once it has been created. However, this does not prevent you from assigning more than one value to a string variable. Consider the following example: + + [!code-vb[VbVbalrStrings#69](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStrings/VB/Class2.vb#69)] + + Here, a string variable is created, given a value, and then its value is changed. + + More specifically, in the first line, an instance of type `String` is created and given the value `This string is immutable`. In the second line of the example, a new instance is created and given the value `Or is it?`, and the string variable discards its reference to the first instance and stores a reference to the new instance. + + Unlike other intrinsic data types, `String` is a reference type. When a variable of reference type is passed as an argument to a function or subroutine, a reference to the memory address where the data is stored is passed instead of the actual value of the string. So in the previous example, the name of the variable remains the same, but it points to a new and different instance of the `String` class, which holds the new value. + ## See also - [Introduction to Strings in Visual Basic](introduction-to-strings.md) - [String Data Type](../../../language-reference/data-types/string-data-type.md) - [Char Data Type](../../../language-reference/data-types/char-data-type.md) -- [Basic String Operations](../../../../standard/base-types/basic-string-operations.md)