博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# String 类
阅读量:6036 次
发布时间:2019-06-20

本文共 85323 字,大约阅读时间需要 284 分钟。


                            Name Description
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Initializes a new instance of the String class to the value indicated by an array of Unicode characters.
Public methodSupported by the XNA Framework Initializes a new instance of the String class to the value indicated by a pointer to an array of 8-bit signed integers.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters, a starting character position within that array, and a length.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Initializes a new instance of the String class to the value indicated by an array of Unicode characters, a starting character position within that array, and a length.
Public method Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting position within that array, and a length.
Public method Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting position within that array, a length, and an object.

                            Name Description
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Gets the object at a specified position in the current String object.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Gets the number of characters in the current String object.

                            Name Description
Public methodSupported by the XNA Framework Returns a reference to this instance of String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Compares two specified String objects and returns an integer that indicates their relative position in the sort order.
Public methodStatic memberSupported by the XNA Framework Compares two specified String objects, ignoring or honoring their case, and returns an integer that indicates their relative position in the sort order.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Compares two specified String objects using the specified rules, and returns an integer that indicates their relative position in the sort order.
Public methodStatic memberSupported by the XNA Framework Compares two specified String objects, ignoring or honoring their case, and using culture-specific information to influence the comparison, and returns an integer that indicates their relative position in the sort order.
Public methodStatic memberSupported by Portable Class Library Compares two specified String objects using the specified comparison options and culture-specific information to influence the comparison, and returns an integer that indicates the relationship of the two strings to each other in the sort order.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Compares substrings of two specified String objects and returns an integer that indicates their relative position in the sort order.
Public methodStatic memberSupported by the XNA Framework Compares substrings of two specified String objects, ignoring or honoring their case, and returns an integer that indicates their relative position in the sort order.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Compares substrings of two specified String objects using the specified rules, and returns an integer that indicates their relative position in the sort order.
Public methodStatic memberSupported by the XNA Framework Compares substrings of two specified String objects, ignoring or honoring their case and using culture-specific information to influence the comparison, and returns an integer that indicates their relative position in the sort order.
Public methodStatic member Compares substrings of two specified String objects using the specified comparison options and culture-specific information to influence the comparison, and returns an integer that indicates the relationship of the two substrings to each other in the sort order.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Compares two specified String objects by evaluating the numeric values of the corresponding objects in each string.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Compares substrings of two specified String objects by evaluating the numeric values of the corresponding objects in each substring.
Public methodSupported by the XNA Framework Compares this instance with a specified and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified .
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Compares this instance with a specified String object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Creates the string  representation of a specified object.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the string representations of the elements in a specified array.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the members of a constructed collection of type String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the elements of a specified String array.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the string representations of two specified objects.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates two specified instances of String.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the string representations of three specified objects.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates three specified instances of String.
Public methodStatic member Concatenates the string representations of four specified objects and any objects specified in an optional variable length parameter list.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates four specified instances of String.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the members of an implementation.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a value indicating whether the specified String object occurs within this string.
Public methodStatic memberSupported by the XNA Framework Creates a new instance of String with the same value as a specified String.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether the end of this string instance matches the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether the end of this string instance matches the specified string when compared using the specified comparison option.
Public method Determines whether the end of this string instance matches the specified string when compared using the specified culture.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether this instance and a specified object, which must also be a String object, have the same value. (Overrides .)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether this instance and another specified String object have the same value.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether two specified String objects have the same value.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether this string and a specified String object have the same value. A parameter specifies the culture, case, and sort rules used in the comparison.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether two specified String objects have the same value. A parameter specifies the culture, case, and sort rules used in the comparison.
Public methodStatic memberSupported by the XNA Framework Replaces one or more format items in a specified string with the string representation of a specified object.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Replaces the format items in a specified string with the string representations of corresponding objects in a specified array. A parameter supplies culture-specific formatting information.
Public methodStatic memberSupported by the XNA Framework Replaces the format items in a specified string with the string representation of two specified objects.
Public methodStatic memberSupported by the XNA Framework Replaces the format items in a specified string with the string representation of three specified objects.
Public methodSupported by the XNA Framework Retrieves an object that can iterate through the individual characters in this string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns the hash code for this string. (Overrides .)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Gets the of the current instance. (Inherited from .)
Public methodSupported by the XNA Framework Returns the for class String.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence of the specified Unicode character in this string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence of the specified string in this instance.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence of the specified Unicode character in this string. The search starts at a specified character position.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence of the specified string in the current String object. A parameter specifies the type of search to use for the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence of the specified character in this instance. The search starts at a specified character position and examines a specified number of character positions.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position and examines a specified number of character positions.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence of the specified string in the current String object. Parameters specify the starting search position in the current string and the type of search to use for the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence of the specified string in the current String object. Parameters specify the starting search position in the current string, the number of characters in the current string to search, and the type of search to use for the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. The search starts at a specified character position.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. The search starts at a specified character position and examines a specified number of character positions.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a new string in which a specified string is inserted at a specified index position in this instance.
Public methodStatic memberSupported by the XNA Framework Retrieves the system's reference to the specified String.
Public methodStatic memberSupported by the XNA Framework Retrieves a reference to a specified String.
Public method Indicates whether this string is in Unicode normalization form C.
Public method Indicates whether this string is in the specified Unicode normalization form.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Indicates whether the specified string is null or an string.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store apps Indicates whether a specified string is null, empty, or consists only of white-space characters.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the members of a constructed collection of type String, using the specified separator between each member.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the elements of an object array, using the specified separator between each element.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates all the elements of a string array, using the specified separator between each element.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the specified elements of a string array, using the specified separator between each element.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store apps Concatenates the members of a collection, using the specified separator between each member.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence of a specified Unicode character within this instance.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence of a specified string within this instance.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence of a specified Unicode character within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence of a specified string within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the last occurrence of a specified string within the current String object. A parameter specifies the type of search to use for the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence of the specified Unicode character in a substring within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string for a specified number of character positions.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence of a specified string within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string for a specified number of character positions.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index of the last occurrence of a specified string within the current String object. The search starts at a specified character position and proceeds backward toward the beginning of the string. A parameter specifies the type of comparison to perform when searching for the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence of a specified string within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string for the specified number of character positions. A parameter specifies the type of comparison to perform when searching for the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array. The search starts at a specified character position and proceeds backward toward the beginning of the string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array. The search starts at a specified character position and proceeds backward toward the beginning of the string for a specified number of character positions.
Public method Returns a new string whose textual value is the same as this string, but whose binary representation is in Unicode normalization form C.
Public method Returns a new string whose textual value is the same as this string, but whose binary representation is in the specified Unicode normalization form.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a new string that right-aligns the characters in this instance by padding them with spaces on the left, for a specified total length.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a new string that right-aligns the characters in this instance by padding them on the left with a specified Unicode character, for a specified total length.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a new string that left-aligns the characters in this string by padding them with spaces on the right, for a specified total length.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a new string in which a specified number of characters in the current this instance beginning at a specified position have been deleted.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a new string in which all occurrences of a specified Unicode character in this instance are replaced with another specified Unicode character.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array. A parameter specifies the maximum number of substrings to return.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a string array that contains the substrings in this string that are delimited by elements of a specified Unicode character array. A parameter specifies whether to return empty array elements.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. A parameter specifies whether to return empty array elements.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a string array that contains the substrings in this string that are delimited by elements of a specified Unicode character array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether the beginning of this string instance matches the specified string.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether the beginning of this string instance matches the specified string when compared using the specified comparison option.
Public method Determines whether the beginning of this string instance matches the specified string when compared using the specified culture.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Retrieves a substring from this instance. The substring starts at a specified character position.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Copies the characters in this instance to a Unicode character array.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Copies the characters in a specified substring in this instance to a Unicode character array.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a copy of this string converted to lowercase.
Public methodSupported by the XNA Framework Returns a copy of this string converted to lowercase, using the casing rules of the specified culture.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a copy of this String object converted to lowercase using the casing rules of the invariant culture.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns this instance of String; no actual conversion is performed. (Overrides .)
Public methodSupported by the XNA Framework Returns this instance of String; no actual conversion is performed.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a copy of this string converted to uppercase.
Public methodSupported by the XNA Framework Returns a copy of this string converted to uppercase, using the casing rules of the specified culture.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns a copy of this String object converted to uppercase using the casing rules of the invariant culture.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Removes all leading and trailing white-space characters from the current String object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Removes all trailing occurrences of a set of characters specified in an array from the current String object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Removes all leading occurrences of a set of characters specified in an array from the current String object.

                            Name Description
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether two specified strings have the same value.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Determines whether two specified strings have different values.

                            Name Description
Public Extension MethodSupported by the XNA Framework Overloaded.  Applies an accumulator function over a sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Determines whether all elements of a sequence satisfy a condition. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Determines whether a sequence contains any elements. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Determines whether any element of a sequence satisfies a condition. (Defined by .)
Public Extension MethodSupported by the XNA Framework Returns the input typed as . (Defined by .)
Public Extension MethodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Overloaded.  Enables parallelization of a query. (Defined by .)
Public Extension Method Overloaded.  Enables parallelization of a query. (Defined by .)
Public Extension MethodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Overloaded.  Converts an to an . (Defined by .)
Public Extension Method Overloaded.  Converts a generic to a generic . (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the average of a sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Casts the elements of an to the specified type. (Defined by .)
Public Extension MethodSupported by the XNA Framework Concatenates two sequences. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Determines whether a sequence contains a specified element by using the default equality comparer. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Determines whether a sequence contains a specified element by using a specified . (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the number of elements in a sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns a number that represents how many elements in the specified sequence satisfy a condition. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the elements of the specified sequence or the type parameter's default value in a singleton collection if the sequence is empty. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns distinct elements from a sequence by using the default equality comparer to compare values. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns distinct elements from a sequence by using a specified to compare values. (Defined by .)
Public Extension MethodSupported by the XNA Framework Returns the element at a specified index in a sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Returns the element at a specified index in a sequence or a default value if the index is out of range. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Produces the set difference of two sequences by using the default equality comparer to compare values. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Produces the set difference of two sequences by using the specified to compare values. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the first element of a sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the first element in a sequence that satisfies a specified condition. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the first element of a sequence, or a default value if the sequence contains no elements. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the first element of the sequence that satisfies a condition or a default value if no such element is found. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Groups the elements of a sequence according to a specified key selector function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Groups the elements of a sequence according to a key selector function. The keys are compared by using a comparer and each group's elements are projected by using a specified function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. The keys are compared by using a specified comparer. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. The elements of each group are projected by using a specified function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. Key values are compared by using a specified comparer, and the elements of each group are projected by using a specified function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Correlates the elements of two sequences based on equality of keys and groups the results. The default equality comparer is used to compare keys. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Correlates the elements of two sequences based on key equality and groups the results. A specified is used to compare keys. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Produces the set intersection of two sequences by using the default equality comparer to compare values. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Produces the set intersection of two sequences by using the specified to compare values. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Correlates the elements of two sequences based on matching keys. The default equality comparer is used to compare keys. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Correlates the elements of two sequences based on matching keys. A specified is used to compare keys. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the last element of a sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the last element of a sequence that satisfies a specified condition. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the last element of a sequence, or a default value if the sequence contains no elements. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the last element of a sequence that satisfies a condition or a default value if no such element is found. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns an that represents the total number of elements in a sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns an that represents how many elements in a sequence satisfy a condition. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the maximum value in a generic sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the maximum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a generic sequence and returns the maximum resulting value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the minimum value in a generic sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a sequence and returns the minimum nullable value. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Invokes a transform function on each element of a generic sequence and returns the minimum resulting value. (Defined by .)
Public Extension MethodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Filters the elements of an based on a specified type. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Sorts the elements of a sequence in ascending order according to a key. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Sorts the elements of a sequence in ascending order by using a specified comparer. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Sorts the elements of a sequence in descending order according to a key. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Sorts the elements of a sequence in descending order by using a specified comparer. (Defined by .)
Public Extension MethodSupported by the XNA Framework Inverts the order of the elements in a sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Projects each element of a sequence into a new form. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Projects each element of a sequence into a new form by incorporating the element's index. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Projects each element of a sequence to an and flattens the resulting sequences into one sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Projects each element of a sequence to an , and flattens the resulting sequences into one sequence. The index of each source element is used in the projected form of that element. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Projects each element of a sequence to an , flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. The index of each source element is used in the intermediate projected form of that element. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Projects each element of a sequence to an , flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Determines whether two sequences are equal by comparing their elements by using a specified . (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition. (Defined by .)
Public Extension MethodSupported by the XNA Framework Bypasses a specified number of elements in a sequence and then returns the remaining elements. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. The element's index is used in the logic of the predicate function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Computes the sum of the sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Returns a specified number of contiguous elements from the start of a sequence. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns elements from a sequence as long as a specified condition is true. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic of the predicate function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Creates an array from a . (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Creates a from an according to a specified key selector function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Creates a from an according to a specified key selector function and key comparer. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Creates a from an according to specified key selector and element selector functions. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Creates a from an according to a specified key selector function, a comparer, and an element selector function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Creates a from an . (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Creates a from an according to a specified key selector function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Creates a from an according to a specified key selector function and key comparer. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Creates a from an according to specified key selector and element selector functions. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Creates a from an according to a specified key selector function, a comparer and an element selector function. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Produces the set union of two sequences by using the default equality comparer. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Produces the set union of two sequences by using a specified . (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Filters a sequence of values based on a predicate. (Defined by .)
Public Extension MethodSupported by the XNA Framework Overloaded.  Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function. (Defined by .)
Public Extension Method Merges two sequences by using the specified predicate function. (Defined by .)

                            Name Description
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Represents the empty string. This field is read-only.

                            Name Description
Explicit interface implemetationPrivate methodSupported by Portable Class LibrarySupported in .NET for Windows Store apps Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Infrastructure. For a description of this member, see .
Explicit interface implemetationPrivate methodSupported by the XNA Framework Returns an enumerator that iterates through the current String object.
Explicit interface implemetationPrivate methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store apps Returns an enumerator that iterates through the current String object.

A string is a sequential collection of Unicode characters that is used to represent text. A String object is a sequential collection of objects that represent a string. The value of the String object is the content of the sequential collection, and that value is immutable (that is, it is read-only). For more information about the immutability of strings, see the section later in this topic. The maximum size of a String object in memory is 2 GB, or about 1 billion characters.

In this section:

Instantiating a String object

You can instantiate a String object in the following ways:

  • By assigning a string literal to a String variable. This is the most commonly used method for creating a string. The following example uses assignment to create several strings. Note that in C#, because the backslash (\) is an escape character, literal backslashes in a string must be escaped or the entire string must be @-quoted.

    C#
    string string1 = "This is a string created by assignment.";Console.WriteLine(string1);string string2a = "The path is C:\\PublicDocuments\\Report1.doc";Console.WriteLine(string2a);string string2b = @"The path is C:\PublicDocuments\Report1.doc";Console.WriteLine(string2b);// The example displays the following output: //       This is a string created by assignment. //       The path is C:\PublicDocuments\Report1.doc //       The path is C:\PublicDocuments\Report1.doc
  • By calling a String class constructor. The following example instantiates strings by calling several class constructors. Note that some of the constructors include pointers to character arrays or signed byte arrays as parameters. Visual Basic does not support calls to these constructors. For detailed information about String constructors, see the constructor summary.

    C#
    char[] chars = { 'w', 'o', 'r', 'd' };sbyte[] bytes = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x00 };// Create a string from a character array. string string1 = new string(chars);Console.WriteLine(string1);// Create a string that consists of a character repeated 20 times. string string2 = new string('c', 20);Console.WriteLine(string2);string stringFromBytes = null;string stringFromChars = null;unsafe{   fixed (sbyte* pbytes = bytes)   {      // Create a string from a pointer to a signed byte array.      stringFromBytes = new string(pbytes);   }   fixed (char* pchars = chars)   {      // Create a string from a pointer to a character array.      stringFromChars = new string(pchars);   }}Console.WriteLine(stringFromBytes);Console.WriteLine(stringFromChars);// The example displays the following output: //       word //       cccccccccccccccccccc //       ABCDE //       word
  • By using the string concatenation operator (+ in C# and & or + in Visual Basic) to create a single string from any combination of String instances and string literals. The following example illustrates the use of the string concatenation operator.

    C#
    string string1 = "Today is " + DateTime.Now.ToString("D") + ".";Console.WriteLine(string1);string string2 = "This is one sentence. " + "This is a second. ";string2 += "This is a third sentence.";Console.WriteLine(string2);// The example displays output like the following: //    Today is Tuesday, July 06, 2011. //    This is one sentence. This is a second. This is a third sentence.
  • By retrieving a property or calling a method that returns a string. The following example uses the methods of the String class to extract a substring from a larger string.

    C#
    string sentence = "This sentence has five words.";// Extract the second word. int startPosition = sentence.IndexOf(" ") + 1;string word2 = sentence.Substring(startPosition,                                  sentence.IndexOf(" ", startPosition) - startPosition);Console.WriteLine("Second word: " + word2);// The example displays the following output: //       Second word: sentence
  • By calling a formatting method to convert a value or object to its string representation. The following example uses the feature to embed the string representation of two objects into a string.

    C#
    DateTime dateAndTime = new DateTime(2011, 7, 6, 7, 32, 0);double temperature = 68.3;string result = String.Format("At {0:t} on {0:D}, the temperature was {1:F1} degrees Fahrenheit.",                              dateAndTime, temperature);Console.WriteLine(result);// The example displays the following output: //       At 7:32 AM on Wednesday, July 06, 2011, the temperature was 68.3 degrees Fahrenheit.

Char objects and Unicode characters

Each character in a string is defined by a Unicode scalar value, also called a Unicode code point or the ordinal (numeric) value of the Unicode character. Each code point is encoded by using UTF-16 encoding, and the numeric value of each element of the encoding is represented by a object.

A single object usually represents a single code point; that is, the numeric value of the equals the code point. For example, the code point for the character "a" is U+0061. However, a code point might require more than one encoded element (more than one object). The Unicode standard defines three types of characters that correspond to multiple objects: graphemes, Unicode supplementary code points, and characters in the supplementary planes.

  • A grapheme is represented by a base character followed by one or more combining characters. For example, the character ä is represented by a object whose code point is U+0061 followed by a object whose code point is U+0308. This character can also be defined by a single object that has a code point of U+00E4. As the following example shows, a culture-sensitive comparison for equality indicates that these two representations are equal, although an ordinary ordinal comparison does not. However, if the two strings are normalized, an ordinal comparison also indicates that they are equal. (For more information on normalizing strings, see the section.)

    C#
    using System;using System.Globalization;using System.IO;public class Example{   public static void Main()   {      StreamWriter sw = new StreamWriter(@".\graphemes.txt");      string grapheme = "\u0061\u0308";      sw.WriteLine(grapheme);      string singleChar = "\u00e4";      sw.WriteLine(singleChar);      sw.WriteLine("{0} = {1} (Culture-sensitive): {2}", grapheme, singleChar,                    String.Equals(grapheme, singleChar,                                  StringComparison.CurrentCulture));      sw.WriteLine("{0} = {1} (Ordinal): {2}", grapheme, singleChar,                    String.Equals(grapheme, singleChar,                                  StringComparison.Ordinal));      sw.WriteLine("{0} = {1} (Normalized Ordinal): {2}", grapheme, singleChar,                    String.Equals(grapheme.Normalize(),                                  singleChar.Normalize(),                                  StringComparison.Ordinal));      sw.Close();    }}// The example produces the following output: //       ä //       ä //       ä = ä (Culture-sensitive): True //       ä = ä (Ordinal): False //       ä = ä (Normalized Ordinal): True
  • A Unicode supplementary code point (a surrogate pair) is represented by a object whose code point is a high surrogate followed by a object whose code point is a low surrogate. The code points of high surrogates range from U+D800 to U+DBFF. The code points of low surrogates range from U+DC00 to U+DFFF. Surrogate pairs are used to represent characters in the 16 Unicode supplementary planes. The following example creates a surrogate character and passes it to the method to determine whether it is a surrogate pair.

    C#
    using System;public class Example{   public static void Main()   {      string surrogate = "\uD800\uDC03";      for (int ctr = 0; ctr < surrogate.Length; ctr++)          Console.Write("U+{0:X2} ", Convert.ToUInt16(surrogate[ctr]));      Console.WriteLine();      Console.WriteLine("   Is Surrogate Pair: {0}",                         Char.IsSurrogatePair(surrogate[0], surrogate[1]));   }}// The example displays the following output: //       U+D800 U+DC03 //          Is Surrogate Pair: True

Strings and embedded null characters

In the .NET Framework, a String object can include embedded null characters, which count as a part of the string's length. However, in some languages such as C and C++, a null character indicates the end of a string; it is not considered a part of the string and is not counted as part of the string's length. This means that the following common assumptions that C and C++ programmers or libraries written in C or C++ might make about strings are not necessarily valid when applied to String objects:

  • The value returned by the strlen or wcslen functions does not necessarily equal .

  • The string created by the strcpy_s or wcscpy_s functions is not necessarily identical to the string created by the method.

You should ensure that native C and C++ code that instantiates String objects, and code that is passed String objects through platform invoke, do not assume that an embedded null character marks the end of the string.

Embedded null characters in a string are also treated differently when a string is sorted (or compared) and when a string is searched. Null characters are ignored when performing culture-sensitive comparisons between two strings, including comparisons using the invariant culture. They are considered only for ordinal or case-insensitive ordinal comparisons. On the other hand, embedded null characters are always considered when searching a string with methods such as , , and .

Strings and indexes

An index is the position of a object (not a Unicode character) in a String. An index is a zero-based, nonnegative number that starts from the first position in the string, which is index position zero. A number of search methods, such as and , return the index of a character or substring in the string instance.

The property lets you access individual objects by their index position in the string. Because the property is the default property (in Visual Basic) or the indexer (in C#), you can access the individual objects in a string by using code such as the following. This code looks for white space or punctuation characters in a string to determine how many words the string contains.

C#
using System;public class Example{   public static void Main()   {      string s1 = "This string consists of a single short sentence.";      int nWords = 0;      s1 = s1.Trim();            for (int ctr = 0; ctr < s1.Length; ctr++) {         if (Char.IsPunctuation(s1[ctr]) | Char.IsWhiteSpace(s1[ctr]))            nWords++;                    }      Console.WriteLine("The sentence\n   {0}\nhas {1} words.",                        s1, nWords);                                                                        }}// The example displays the following output: //       The sentence //          This string consists of a single short sentence. //       has 8 words.

Because the String class implements the interface, you can also iterate through the objects in a string by using a foreach construct, as the following example shows.

C#
using System;public class Example{   public static void Main()   {      string s1 = "This string consists of a single short sentence.";      int nWords = 0;      s1 = s1.Trim();            foreach (var ch in s1) {         if (Char.IsPunctuation(ch) | Char.IsWhiteSpace(ch))            nWords++;                    }      Console.WriteLine("The sentence\n   {0}\nhas {1} words.",                        s1, nWords);                                                                        }}// The example displays the following output: //       The sentence //          This string consists of a single short sentence. //       has 8 words.

Consecutive index values might not correspond to consecutive Unicode characters, because a Unicode character might be encoded as more than one object. To work with Unicode characters instead of objects, use the and classes. The following example illustrates the difference between code that works with objects and code that works with Unicode characters. It compares the number of characters or text elements in each word of a sentence. The string includes two sequences of a base character followed by a combining character.

C#
using System;using System.Collections.Generic;using System.Globalization;public class Example{   public static void Main()   {      // First sentence of The Mystery of the Yellow Room, by Leroux.       string opening = "Ce n'est pas sans une certaine émotion que "+                       "je commence à raconter ici les aventures " +                       "extraordinaires de Joseph Rouletabille.";       // Character counters.       int nChars = 0;      // Objects to store word count.      List
chars = new List
(); List
elements = new List
(); foreach (var ch in opening) { // Skip the ' character.  if (ch == '\u0027') continue; if (Char.IsWhiteSpace(ch) | (Char.IsPunctuation(ch))) { chars.Add(nChars); nChars = 0; } else { nChars++; } } TextElementEnumerator te = StringInfo.GetTextElementEnumerator(opening); while (te.MoveNext()) { string s = te.GetTextElement(); // Skip the ' character.  if (s == "\u0027") continue; if ( String.IsNullOrEmpty(s.Trim()) | (s.Length == 1 && Char.IsPunctuation(Convert.ToChar(s)))) { elements.Add(nChars); nChars = 0; } else { nChars++; } } // Display character counts. Console.WriteLine("{0,6} {1,20} {2,20}", "Word #", "Char Objects", "Characters"); for (int ctr = 0; ctr < chars.Count; ctr++) Console.WriteLine("{0,6} {1,20} {2,20}", ctr, chars[ctr], elements[ctr]); }}// The example displays the following output: // Word # Char Objects Characters // 0 2 2 // 1 4 4 // 2 3 3 // 3 4 4 // 4 3 3 // 5 8 8 // 6 8 7 // 7 3 3 // 8 2 2 // 9 8 8 // 10 2 1 // 11 8 8 // 12 3 3 // 13 3 3 // 14 9 9 // 15 15 15 // 16 2 2 // 17 6 6 // 18 12 12

Null strings and empty strings

A string that has been declared but has not been assigned a value is null. Attempting to call methods on that string throws a . A null string is different from an empty string, which is a string whose value is "" or . In some cases, passing either a null string or an empty string as an argument in a method call throws an exception. For example, passing a null string to the method throws an , and passing an empty string throws a . In other cases, a method argument can be either a null string or an empty string. For example, if you are providing an implementation for a class, you want to equate both a null string and an empty string with the general ("G") format specifier.

The String class includes the following two convenience methods that enable you to test whether a string is null or empty:

  • , which indicates whether a string is either null or is equal to  . This method eliminates the need to use code such as the following:

    C#
    if (str == null || str.Equals(String.Empty))
  • , which indicates whether a string is null, equals , or consists exclusively of white-space characters. This method eliminates the need to use code such as the following:

    C#
    if (str == null || str.Equals(String.Empty) || str.Trim().Equals(String.Empty))

The following example uses the method in the implementation of a custom Temperature class. The method supports the "G", "C", "F", and "K" format strings. If an empty format string or a format string whose value is null is passed to the method, its value is changed to the "G" format string.

C#
public string ToString(string format, IFormatProvider provider) {   if (String.IsNullOrEmpty(format)) format = "G";     if (provider == null) provider = CultureInfo.CurrentCulture;   switch (format.ToUpperInvariant())   {      // Return degrees in Celsius.           case "G":      case "C":         return temp.ToString("F2", provider) + "°C";      // Return degrees in Fahrenheit.       case "F":          return (temp * 9 / 5 + 32).ToString("F2", provider) + "°F";      // Return degrees in Kelvin.       case "K":            return (temp + 273.15).ToString();      default:         throw new FormatException(               String.Format("The {0} format string is not supported.",                              format));   }                                   }

Immutability and the StringBuilder class

A String object is called immutable (read-only), because its value cannot be modified after it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification.

Because strings are immutable, string manipulation routines that perform repeated additions or deletions to what appears to be a single string can exact a significant performance penalty. For example, the following code uses a random number generator to create a string with 1000 characters in the range 0x0001 to 0x052F. Although the code appears to use string concatenation to append a new character to the existing string named str, it actually creates a new String object for each concatenation operation.

C#
using System;using System.IO;using System.Text;public class Example{   public static void Main()   {      Random rnd = new Random();      string str = String.Empty;      StreamWriter sw = new StreamWriter(@".\StringFile.txt",                            false, Encoding.Unicode);      for (int ctr = 0; ctr <= 1000; ctr++) {         str += Convert.ToChar(rnd.Next(1, 0x0530));          if (str.Length % 60 == 0)            str += Environment.NewLine;                }                          sw.Write(str);      sw.Close();   }}

You can use the class instead of the String class for operations that make multiple changes to the value of a string. Unlike instances of the String class, objects are mutable; when you concatenate, append, or delete substrings from a string, the operations are performed on a single string. When you have finished modifying the value of a object, you can call its method to convert it to a string. The following example replaces the String used in the previous example to concatenate 1000 random characters in the range to  0x0001 to 0x052F with a object.

C#
using System;using System.IO;using System.Text;public class Example{   public static void Main()   {      Random rnd = new Random();      StringBuilder sb = new StringBuilder();      StreamWriter sw = new StreamWriter(@".\StringFile.txt",                                          false, Encoding.Unicode);      for (int ctr = 0; ctr <= 1000; ctr++) {         sb.Append(Convert.ToChar(rnd.Next(1, 0x0530)));          if (sb.Length % 60 == 0)            sb.AppendLine();                }                          sw.Write(sb.ToString());      sw.Close();   }}

Ordinal vs. culture-sensitive operations

Members of the String class perform either ordinal or culture-sensitive (linguistic) operations on a String object. An ordinal operation acts on the numeric value of each object. A culture-sensitive operation acts on the value of the String object, and takes culture-specific casing, sorting, formatting, and parsing rules into account. Culture-sensitive operations execute in the context of an explicitly declared culture or the implicit current culture. The two kinds of operations can produce very different results when they are performed on the same string.

Security noteSecurity Note

If your application makes a security decision about a symbolic identifier such as a file name or named pipe, or about persisted data such as the text-based data in an XML file, the operation should use an ordinal comparison instead of a culture-sensitive comparison. This is because a culture-sensitive comparison can yield different results depending on the culture in effect, whereas an ordinal comparison depends solely on the binary value of the compared characters.

Important noteImportant

Most methods that perform string operations include an overload that has a parameter of type , which enables you to specify whether the method performs an ordinal or culture-sensitive operation. In general, you should call this overload to make the intent of your method call clear. For best practices and guidance for using ordinal and culture-sensitive operations on strings, see .

Operations for , , , and can be either ordinal or culture-sensitive. The following sections discuss each category of operation.


Casing rules determine how to change the capitalization of a Unicode character; for example, from lowercase to uppercase. Often, a casing operation is performed before a string comparison. For example, a string might be converted to uppercase so that it can be compared with another uppercase string. You can convert the characters in a string to lowercase by calling the or method, and you can convert them to uppercase by calling the or method. In addition, you can use the method to convert a string to title case.

Casing operations can be based on the rules of the current culture, a specified culture, or the invariant culture.  The following example illustrates some of the differences in casing rules between cultures when converting strings to uppercase.

C#
using System;using System.Globalization;using System.IO;public class Example{   public static void Main()   {      StreamWriter sw = new StreamWriter(@".\case.txt");         string[] words = { "file", "sıfır", "Dženana" };      CultureInfo[] cultures = { CultureInfo.InvariantCulture,                                  new CultureInfo("en-US"),                                   new CultureInfo("tr-TR") };      foreach (var word in words) {         sw.WriteLine("{0}:", word);         foreach (var culture in cultures) {            string name = String.IsNullOrEmpty(culture.Name) ?                                  "Invariant" : culture.Name;            string upperWord = word.ToUpper(culture);            sw.WriteLine("   {0,10}: {1,7} {2, 38}", name,                          upperWord, ShowHexValue(upperWord));         }         sw.WriteLine();        }      sw.Close();   }   private static string ShowHexValue(string s)   {      string retval = null;      foreach (var ch in s) {         byte[] bytes = BitConverter.GetBytes(ch);         retval += String.Format("{0:X2} {1:X2} ", bytes[1], bytes[0]);           }      return retval;   } }// The example displays the following output: //    file: //        Invariant:    FILE               00 46 00 49 00 4C 00 45  //            en-US:    FILE               00 46 00 49 00 4C 00 45  //            tr-TR:    FİLE               00 46 01 30 00 4C 00 45  //     //    sıfır: //        Invariant:   SıFıR         00 53 01 31 00 46 01 31 00 52  //            en-US:   SIFIR         00 53 00 49 00 46 00 49 00 52  //            tr-TR:   SIFIR         00 53 00 49 00 46 00 49 00 52  //     //    Dženana: //        Invariant:  DžENANA   01 C5 00 45 00 4E 00 41 00 4E 00 41  //            en-US:  DŽENANA   01 C4 00 45 00 4E 00 41 00 4E 00 41  //            tr-TR:  DŽENANA   01 C4 00 45 00 4E 00 41 00 4E 00 41

Formatting and parsing are inverse operations. Formatting rules determine how to convert a value, such as a date and time or a number, to its string representation, whereas parsing rules determine how to convert a string representation to a value such as a date and time. Both formatting and parsing rules are dependent on cultural conventions. The following example illustrates the ambiguity that can arise when interpreting a culture-specific date string. Without knowing the conventions of the culture that was used to produce a date string, it is not possible to know whether 03/01/2011, 3/1/2011, and 01/03/2011 represent January 3, 2011 or March 1, 2011.

C#
using System;using System.Globalization;public class Example{   public static void Main()   {      DateTime date = new DateTime(2011, 3, 1);      CultureInfo[] cultures = { CultureInfo.InvariantCulture,                                  new CultureInfo("en-US"),                                  new CultureInfo("fr-FR") };      foreach (var culture in cultures)         Console.WriteLine("{0,-12} {1}", String.IsNullOrEmpty(culture.Name) ?                           "Invariant" : culture.Name,                            date.ToString("d", culture));                                       }}// The example displays the following output: //       Invariant    03/01/2011 //       en-US        3/1/2011 //       fr-FR        01/03/2011

Similarly, as the following example shows, a single string can produce different dates depending on the culture whose conventions are used in the parsing operation.

C#
using System;using System.Globalization;public class Example{   public static void Main()   {      string dateString = "07/10/2011";      CultureInfo[] cultures = { CultureInfo.InvariantCulture,                                  CultureInfo.CreateSpecificCulture("en-GB"),                                  CultureInfo.CreateSpecificCulture("en-US") };      Console.WriteLine("{0,-12} {1,10} {2,8} {3,8}\n", "Date String", "Culture",                                                  "Month", "Day");      foreach (var culture in cultures) {         DateTime date = DateTime.Parse(dateString, culture);         Console.WriteLine("{0,-12} {1,10} {2,8} {3,8}", dateString,                            String.IsNullOrEmpty(culture.Name) ?                           "Invariant" : culture.Name,                            date.Month, date.Day);      }                         }}// The example displays the following output: //       Date String     Culture    Month      Day //        //       07/10/2011    Invariant        7       10 //       07/10/2011        en-GB       10        7 //       07/10/2011        en-US        7       10

Sort rules determine the alphabetic order of Unicode characters and how two strings compare to each other. For example, the method compares two strings based on the parameter. If the parameter value is , the method performs a linguistic comparison that uses the conventions of the current culture; if the parameter value is , the method performs an ordinal comparison. Consequently, as the following example shows, if the current culture is U.S. English, the first call to the method (using culture-sensitive comparison) considers "a" less than "A", but the second call to the same method (using ordinal comparison) considers "a" greater than "A".

C#
using System;using System.Globalization;using System.Threading;public class Example{   public static void Main()   {      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");      Console.WriteLine(String.Compare("A", "a", StringComparison.CurrentCulture));      Console.WriteLine(String.Compare("A", "a", StringComparison.Ordinal));   }}// The example displays the following output: //       1 //       -32

The .NET Framework supports word, string, and ordinal sort rules:

  • A word sort performs a culture-sensitive comparison of strings in which certain nonalphanumeric Unicode characters might have special weights assigned to them. For example, the hyphen (-) might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list. For a list of the String methods that compare two strings using word sort rules, see the section.

  • A string sort also performs a culture-sensitive comparison. It is similar to a word sort, except that there are no special cases, and all nonalphanumeric symbols come before all alphanumeric Unicode characters. Two strings can be compared using string sort rules by calling the method overloads that have an options parameter that is supplied a value of . Note that this is the only method that the .NET Framework provides to compare two strings using string sort rules.

  • An ordinal sort compares strings based on the numeric value of each object in the string. An ordinal comparison is automatically case-sensitive because the lowercase and uppercase versions of a character have different code points. However, if case is not important, you can specify an ordinal comparison that ignores case. This is equivalent to converting the string to uppercase by using the invariant culture and then performing an ordinal comparison on the result. For a list of the String methods that compare two strings using ordinal sort rules, see the section.

A culture-sensitive comparison is any comparison that explicitly or implicitly uses a object, including the invariant culture that is specified by the property. The implicit culture is the current culture, which is specified by the and properties. A culture-sensitive comparison is generally appropriate for sorting, whereas an ordinal comparison is not. An ordinal comparison is generally appropriate for determining whether two strings are equal (that is, for determining identity), whereas a culture-sensitive comparison is not.

NoteNote

The culture-sensitive sorting and casing rules used in string comparison depend on the version of the .NET Framework. In the .NET Framework 4.5 running on the Windows 8 operating system, sorting, casing, normalization, and Unicode character information conforms to the Unicode 6.0 standard. On other operating systems, it conforms to the Unicode 5.0 standard.

For more information about word, string, and ordinal sort rules, see the topic. For recommendations on when to use each rule, see .

Ordinarily, you do not call string comparison methods such as directly to determine the sort order of strings. Instead, comparison methods are called by sorting methods such as or . The following example performs four different sorting operations (word sort using the current culture, word sort using the invariant culture, ordinal sort, and string sort using the invariant culture) without explicitly calling a string comparison method. Note that each type of sort produces a unique ordering of strings in its array.

C#
using System;using System.Collections;using System.Collections.Generic;using System.Globalization;public class Example{   public static void Main()   {      string[] strings = { "coop", "co-op", "cooperative",                            "co\u00ADoperative", "cœur", "coeur" };      // Perform a word sort using the current (en-US) culture.       string[] current = new string[strings.Length];       strings.CopyTo(current, 0);       Array.Sort(current, StringComparer.CurrentCulture);      // Perform a word sort using the invariant culture.       string[] invariant = new string[strings.Length];      strings.CopyTo(invariant, 0);       Array.Sort(invariant, StringComparer.InvariantCulture);      // Perform an ordinal sort.       string[] ordinal = new string[strings.Length];      strings.CopyTo(ordinal, 0);       Array.Sort(ordinal, StringComparer.Ordinal);      // Perform a string sort using the current culture.       string[] stringSort = new string[strings.Length];      strings.CopyTo(stringSort, 0);       Array.Sort(stringSort, new SCompare());      // Display array values      Console.WriteLine("{0,13} {1,13} {2,15} {3,13} {4,13}\n",                         "Original", "Word Sort", "Invariant Word",                         "Ordinal Sort", "String Sort");      for (int ctr = 0; ctr < strings.Length; ctr++)         Console.WriteLine("{0,13} {1,13} {2,15} {3,13} {4,13}",                            strings[ctr], current[ctr], invariant[ctr],                            ordinal[ctr], stringSort[ctr] );             }}// IComparer
implementation to perform string sort. internal class SCompare : IComparer
{ public int Compare(string x, string y) { return CultureInfo.CurrentCulture.CompareInfo.Compare(x, y, CompareOptions.StringSort); }}// The example displays the following output: // Original Word Sort Invariant Word Ordinal Sort String Sort //  // coop cœur cœur co-op co-op // co-op coeur coeur coeur cœur // cooperative coop coop coop coeur // co­operative co-op co-op cooperative coop // cœur cooperative cooperative co­operative cooperative // coeur co­operative co­operative cœur co­operative
Caution noteCaution

If your primary purpose in comparing strings is to determine whether they are equal, you should call the method. Typically, you should use to perform an ordinal comparison. The method is intended primarily to sort strings.

String search methods, such as and , also can perform culture-sensitive or ordinal string comparisons. The following example illustrates the differences between ordinal and culture-sensitive comparisons using the method. A culture-sensitive search in which the current culture is English (United States) considers the substring "oe" to match the ligature "œ". Because a soft hyphen (U+00AD) is a zero-width character, the search treats the soft hyphen as equivalent to and finds a match at the beginning of the string. An ordinal search, on the other hand, does not find a match in either case.

C#
using System;public class Example{   public static void Main()   {      // Search for "oe" and "œu" in "œufs" and "oeufs".      string s1 = "œufs";      string s2 = "oeufs";      FindInString(s1, "oe", StringComparison.CurrentCulture);      FindInString(s1, "oe", StringComparison.Ordinal);      FindInString(s2, "œu", StringComparison.CurrentCulture);      FindInString(s2, "œu", StringComparison.Ordinal);      Console.WriteLine();      string s3 = "co\u00ADoperative";      FindInString(s3, "\u00AD", StringComparison.CurrentCulture);      FindInString(s3, "\u00AD", StringComparison.Ordinal);   }   private static void FindInString(string s, string substring, StringComparison options)   {      int result = s.IndexOf(substring, options);      if (result != -1)         Console.WriteLine("'{0}' found in {1} at position {2}",                            substring, s, result);      else         Console.WriteLine("'{0}' not found in {1}",                            substring, s);                                                     }}// The example displays the following output: //       'oe' found in œufs at position 0 //       'oe' not found in œufs //       'œu' found in oeufs at position 0 //       'œu' not found in oeufs //        //       '­' found in co­operative at position 0 //       '­' found in co­operative at position 2

Use the method to determine the relationship of two strings in the sort order. Typically, this is a culture-sensitive operation. In contrast, call the method to test for equality. Because the test for equality usually compares user input with some known string, such as a valid user name, a password, or a file system path, it is typically an ordinal operation.

Caution noteCaution

It is possible to test for equality by calling the method and determining whether the return value is zero. However, this practice is not recommended. To determine whether two strings are equal, you should call one of the overloads of the method. The preferred overload to call is either the instance method or the static method, because both methods include a parameter that explicitly specifies the type of comparison.

The following example illustrates the danger of performing a culture-sensitive comparison for equality when an ordinal one should be used instead. In this case, the intent of the code is to prohibit file system access from URLs that begin with "FILE://" or "file://" by performing a case-insensitive comparison of the beginning of a URL with the string "FILE://". However, if a culture-sensitive comparison is performed using the Turkish (Turkey) culture on a URL that begins with "file://", the comparison for equality fails, because the Turkish uppercase equivalent of the lowercase "i" is "İ" instead of "I". As a result, file system access is inadvertently permitted. On the other hand, if an ordinal comparison is performed, the comparison for equality succeeds, and file system access is denied.

C#
using System;using System.Globalization;using System.Threading;public class Example{   public static void Main()   {      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR");            string filePath = "file://c:/notes.txt";      Console.WriteLine("Culture-sensitive test for equality:");      if (! TestForEquality(filePath, StringComparison.CurrentCultureIgnoreCase))         Console.WriteLine("Access to {0} is allowed.", filePath);      else         Console.WriteLine("Access to {0} is not allowed.", filePath);      Console.WriteLine("\nOrdinal test for equality:");      if (! TestForEquality(filePath, StringComparison.OrdinalIgnoreCase))         Console.WriteLine("Access to {0} is allowed.", filePath);      else         Console.WriteLine("Access to {0} is not allowed.", filePath);   }   private static bool TestForEquality(string str, StringComparison cmp)   {      int position = str.IndexOf("://");      if (position < 0) return false;      string substring = str.Substring(0, position);        return substring.Equals("FILE", cmp);   }}// The example displays the following output: //       Culture-sensitive test for equality: //       Access to file://c:/notes.txt is allowed. //        //       Ordinal test for equality: //       Access to file://c:/notes.txt is not allowed.

Normalization

Some Unicode characters have multiple representations. For example, any of the following code points can represent the letter "ắ":

  • U+1EAF

  • U+0103 U+0301

  • U+0061 U+0306 U+0301

Multiple representations for a single character complicate searching, sorting, matching, and other string operations.

The Unicode standard defines a process called normalization that returns one binary representation of a Unicode character for any of its equivalent binary representations. Normalization can use several algorithms, called normalization forms, that follow different rules. The .NET Framework supports Unicode normalization forms C, D, KC, and KD. When strings have been normalized to the same normalization form, they can be compared by using ordinal comparison. For more information about normalization and normalization forms, see .

You can determine whether a string is normalized to normalization form C by calling the method, or you can call the method to determine whether a string is normalized to a specified normalization form. You can also call the method to convert a string to normalization form C, or you can call the method to convert a string to a specified normalization form.

The following example illustrates string normalization. It defines the letter "ố" in three different ways in three different strings, and uses an ordinal comparison for equality to determine that each string differs from the other two strings. It then converts each string to the supported normalization forms, and again performs an ordinal comparison of each string in a specified normalization form. In each case, the second test for equality shows that the strings are equal.

C#
using System;using System.Globalization;using System.IO;using System.Text;public class Example{   private static StreamWriter sw;   public static void Main()   {      sw = new StreamWriter(@".\TestNorm1.txt");      // Define three versions of the same word.        string s1 = "sống";        // create word with U+1ED1       string s2 = "s\u00F4\u0301ng";      string s3 = "so\u0302\u0301ng";      TestForEquality(s1, s2, s3);            sw.WriteLine();      // Normalize and compare strings using each normalization form.       foreach (string formName in Enum.GetNames(typeof(NormalizationForm)))      {         sw.WriteLine("Normalization {0}:\n", formName);          NormalizationForm nf = (NormalizationForm) Enum.Parse(typeof(NormalizationForm), formName);         string[] sn = NormalizeStrings(nf, s1, s2, s3);         TestForEquality(sn);                    sw.WriteLine("\n");                                              }      sw.Close();      }   private static void TestForEquality(params string[] words)   {      for (int ctr = 0; ctr <= words.Length - 2; ctr++)         for (int ctr2 = ctr + 1; ctr2 <= words.Length - 1; ctr2++)             sw.WriteLine("{0} ({1}) = {2} ({3}): {4}",                          words[ctr], ShowBytes(words[ctr]),                         words[ctr2], ShowBytes(words[ctr2]),                         words[ctr].Equals(words[ctr2], StringComparison.Ordinal));   }   private static string ShowBytes(string str)   {      string result = null;      foreach (var ch in str)         result += String.Format("{0} ", Convert.ToUInt16(ch).ToString("X4"));       return result.Trim();               }    private static string[] NormalizeStrings(NormalizationForm nf, params string[] words)   {      for (int ctr = 0; ctr < words.Length; ctr++)         if (! words[ctr].IsNormalized(nf))            words[ctr] = words[ctr].Normalize(nf);       return words;      }}// The example displays the following output: //       sống (0073 1ED1 006E 0067) = sống (0073 00F4 0301 006E 0067): False //       sống (0073 1ED1 006E 0067) = sống (0073 006F 0302 0301 006E 0067): False //       sống (0073 00F4 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): False //        //       Normalization FormC: //        //       sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True //       sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True //       sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True //        //        //       Normalization FormD: //        //       sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True //       sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True //       sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True //        //        //       Normalization FormKC: //        //       sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True //       sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True //       sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True //        //        //       Normalization FormKD: //        //       sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True //       sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True //       sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True

String operations by category

The String class provides members for comparing strings, testing strings for equality, finding characters or substrings in a string, modifying a string, extracting substrings from a string, combining strings, formatting values, copying a string, and normalizing a string.


You can compare strings to determine their relative position in the sort order by using the following String methods:

  • returns an integer that indicates the relationship of one string to a second string in the sort order.

  • returns an integer that indicates the relationship of one string to a second string based on a comparison of their code points.

  • returns an integer that indicates the relationship of the current string instance to a second string in the sort order. The method provides the and implementations for the String class.


You call the method to determine whether two strings are equal. The instance and the static overloads let you specify whether the comparison is culture-sensitive or ordinal, and whether case is considered or ignored. Most tests for equality are ordinal, and comparisons for equality that determine access to a system resource (such as a file system object) should always be ordinal.


The String class includes two kinds of search methods:

  • Methods that return a value to indicate whether a particular substring is present in a string instance. These include the , , and methods.

  • Methods that indicate the starting position of a substring in a string instance. These include the , , , and methods.

Caution noteCaution

If you want to search a string for a particular pattern rather than a specific substring, you should use regular expressions. For more information, see .


The String class includes the following methods that appear to modify the value of a string:

  • inserts a string into the current String instance.

  • inserts one or more occurrences of a specified character at the beginning of a string.

  • inserts one or more occurrences of a specified character at the beginning of a string.

  • deletes a substring from the current String instance.

  • replaces a substring with another substring in the current String instance.

  • and convert all the characters in a string to lowercase.

  • and convert all the characters in a string to uppercase.

  • removes all occurrences of a character from the beginning and end of a string.

  • removes all occurrences of a character from the end of a string.

  • removes all occurrences of a character from the beginning of a string.

Important noteImportant

All string modification methods return a new String object. They do not modify the value of the current instance.


The method separates a single string into multiple strings. Overloads of the method allow you to specify multiple delimiters, to determine the maximum number of substrings that the method extracts, and to determine whether empty strings (which occur when delimiters are adjacent) are included among the returned strings.


The following String methods can be used for string concatenation:

  • combines one or more substrings into a single string.

  • concatenates one or more substrings into a single element and adds a separator between each substring.


The method uses the composite formatting feature to replace one or more placeholders in a string with the string representation of some object or value. The method is often used to do the following:

  • To embed the string representation of a numeric value in a string.

  • To embed the string representation of a date and time value in a string.

  • To embed the string representation of an enumeration value in a string.

  • To embed the string representation of some object that supports the interface in a string.

  • To right-justify or left-justify a substring in a field within a larger string.

For detailed information about formatting operations and examples, see the overload summary.


You can call the following String methods to make a copy of a string:

  • returns a reference to an existing String object.

  • creates a copy of an existing string.

  • copies a portion of a string to a character array.


In Unicode, a single character can have multiple code points. Normalization converts these equivalent characters into the same binary representation. The method performs the normalization, and the method determines whether a string is normalized.


.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

  The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .   


This type is thread safe.


Reference

Other Resources

    
        Did you find this helpful?             Yes             No                
                                 Not accurate                            
                                 Not enough depth                            
                                 Need more code examples                            
Tell us more...                
(1500 characters remaining)                                                  
 
 
© 2013 Microsoft. All rights reserved.
                                                                                                 &amp;amp;lt;div&amp;amp;gt;&amp;amp;lt;img alt="DCSIMG" id="Img1" width="1" height="1" src="http://m.webtrends.com/dcsmgru7m99k7mqmgrhudo0k8_8c6m/njs.gif?dcsuri=/nojavascript&amp;amp;amp;amp;WT.js=No" /&amp;amp;gt;&amp;amp;lt;/div&amp;amp;gt;&amp;amp;lt;a href="http://www.omniture.com" title="Web Analytics"&amp;amp;gt; &amp;amp;lt;img src="//msstonojsmsdn.112.2o7.net/b/ss/msstonojsmsdn/1/H.20.2--NS/0" height="1" width="1" border="0" alt="" /&amp;amp;gt; &amp;amp;lt;/a&amp;amp;gt;

转载于:https://www.cnblogs.com/viviancc/archive/2013/04/01/2993387.html

你可能感兴趣的文章
laravel 集合接口
查看>>
C/C++二进制读写png文件
查看>>
thymleaf 常用th 标签
查看>>
RTB 广告系统
查看>>
Linux signal 那些事儿(2)【转】
查看>>
InfluxDB安装及配置
查看>>
Dynamics CRM Microsoft SQL Server 指定的数据库具有更高的版本号
查看>>
PAT Perfect Sequence (25)
查看>>
java.exe进程来源排查录
查看>>
点滴记录——Ubuntu 14.04中Solr与Tomcat整合安装
查看>>
C++实现KMP模式匹配算法
查看>>
ubuntu linux下建立stm32开发环境: GCC安装以及工程Makefile建立
查看>>
记录锁
查看>>
JSONObject与JSONArray的使用
查看>>
[SQL Server] 数据库日志文件自动增长导致连接超时的分析
查看>>
【常见Web应用安全问题】---6、Script source code disclosure
查看>>
<html:form>标签
查看>>
除了《一无所有》,我一无所有
查看>>
每日英语:China Seeks to Calm Anxiety Over Rice
查看>>
C++中struct和class的区别 [转]
查看>>