在C#,字符串是Unicode字符或字符数组的序列。
Unicode字符的范围是U + 0000到U + FFFF 。字符数组也称为文本。因此,字符串是文本的表示形式。字符串由类System.String表示。
字符串类在.NET基类库中定义。换句话说,String对象是System.Char对象的顺序集合,它们表示一个字符串。内存中String对象的最大大小可以为2GB或大约10亿个字符。
字符串类的特征:
- System.String类是不可变的,即一旦创建,其状态就无法更改。
- 借助length属性,它提供了给定字符串存在的字符总数。
- 字符串对象可以包含一个空字符,该字符将算作字符串长度的一部分。
- 它提供了给定字符串字符的位置。
- 它允许空字符串。空字符串是包含零个字符的String对象的有效实例。
- 已声明但尚未分配值的字符串为null。尝试在该字符串上调用方法将引发NullReferenceException 。
- 它还支持搜索字符串,字符串的比较,测试平等,修改字符串,字符串的规范化,字符串的复制,等等。
- 它还提供了几种创建字符串的方式,例如使用构造函数,使用串联等。
建设者
Constructor | Description |
---|---|
String(Char*) | Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters. |
String(Char*, Int32, Int32) | 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. |
String(Char, Int32) | Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times. |
String(Char[]) | Initializes a new instance of the String class to the value indicated by an array of Unicode characters. |
String(Char[], Int32, Int32) | 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. |
String(SByte*) | Initializes a new instance of the String class to the value indicated by a pointer to an array of 8-bit signed integers. |
String(SByte*, Int32, Int32) | 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. |
String(SByte*, Int32, Int32, Encoding) | 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 Encoding object. |
例子:
// C# program to demonstrate the creation
// of string using the constructor
using System;
class Geeks {
// Main Method
public static void Main()
{
char[] chars = { 'G', 'E', 'E', 'K', 'S' };
// Create a string from a character array.
string str1 = new string(chars);
Console.WriteLine(str1);
// Create a string that consists of
// a character repeated 5 times.
string str2 = new string('E', 5);
Console.WriteLine(str2);
}
}
输出:
GEEKS
EEEEE
特性
Property | Description |
---|---|
Chars[Int32] | Gets the Char object at a specified position in the current String object. |
Length | Gets the number of characters in the current String object. |
例子:
// C# program to demonstrate the
// String Class Properties
using System;
class Geeks {
// Main Method
public static void Main()
{
string str = "GeeksforGeeks";
// using Chars[Int32] & Length property
for (int i = 0; i <= str.Length - 1; i++)
Console.Write("{0} ", str[i]);
}
}
输出:
G e e k s f o r G e e k s
方法
Method | Description |
---|---|
Clone() | Returns a reference to this instance of String. |
Compare() | Used to compare the two string objects. |
CompareOrdinal(String, Int32, String, Int32, Int32) | Compares substrings of two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring. |
CompareOrdinal(String, String) | Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string. |
CompareTo() | Compare the current instance with a specified Object or String object. |
Concat() | Concatenates one or more instances of String, or the String representations of the values of one or more instances of Object. |
Contains(String) | Returns a value indicating whether a specified substring occurs within this string. |
Copy(String) | Creates a new instance of String with the same value as a specified String. |
CopyTo(Int32, Char[], Int32, Int32) | Copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters. |
EndsWith() | Determines whether the end of this string instance matches a specified string. |
Equals() | Determines whether two String objects have the same value. |
Format() | Converts the value of objects to strings based on the formats specified and inserts them into another string. |
GetEnumerator() | Retrieves an object that can iterate through the individual characters in this string. |
GetHashCode() | Returns the hash code for this string. |
GetType() | Gets the Type of the current instance. (Inherited from Object) |
GetTypeCode() | Returns the TypeCode for class String. |
IndexOf() | Reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance. |
IndexOfAny() | Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters. The method returns -1 if the characters in the array are not found in this instance. |
Insert(Int32, String) | Returns a new string in which a specified string is inserted at a specified index position in this instance. |
Intern(String) | Retrieves the system’s reference to the specified String. |
IsInterned(String) | Retrieves a reference to a specified String. |
IsNormalized() | Indicates whether this string is in a particular Unicode normalization form. |
IsNullOrEmpty(String) | Indicates whether the specified string is null or an Empty string. |
IsNullOrWhiteSpace(String) | Indicates whether a specified string is null, empty, or consists only of white-space characters. |
Join() | Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. |
LastIndexOf() | Reports the zero-based index position of the last occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. (Inherited from Object) |
Normalize() | Returns a new string whose binary representation is in a particular Unicode normalization form. |
PadLeft() | Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character. |
PadRight() | Returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character. |
Remove() | Returns a new string in which a specified number of characters from the current string are deleted. |
Replace() | Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String. |
Split() | Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array. |
StartsWith(String) | Determines whether the beginning of this string instance matches a specified string. |
Substring(Int32) | Retrieves a substring from this instance. |
ToCharArray() | Copies the characters in this instance to a Unicode character array. |
ToLower() | Returns a copy of this string converted to lowercase. |
ToLowerInvariant() | Returns a copy of this String object converted to lowercase using the casing rules of the invariant culture. |
ToString() | Converts the value of this instance to a String. |
ToUpper() | Returns a copy of this string converted to uppercase. |
ToUpperInvariant() | Returns a copy of this String object converted to uppercase using the casing rules of the invariant culture. |
Trim() | Returns a new string in which all leading and trailing occurrences of a set of specified characters from the current String object are removed. |
TrimEnd(Char[]) | Removes all trailing occurrences of a set of characters specified in an array from the current String object. |
TrimStart(Char[]) | Removes all leading occurrences of a set of characters specified in an array from the current String object. |
例子:
// C# program to illustrate
// String class methods
using System;
class GFG {
static void copymethod()
{
string str1 = "GeeksforGeeks";
string str2 = "geeks";
Console.WriteLine("Original Strings: str1 = "
+ "'{0}' and str2 ='{1}'",
str1, str2);
Console.WriteLine("");
Console.WriteLine("After Copy method");
Console.WriteLine("");
// using the Copy method
// to copy the value of str1
// into str2
str2 = String.Copy(str1);
Console.WriteLine("Strings are str1 = "
+ "'{0}' and str2='{1}'",
str1, str2);
}
// Main method
static public void Main()
{
// variables
string str1 = "geeksforgeeks";
string str2 = "geeksforgeeks";
bool result;
// Compare(string, string) method return true
// because the given strings are equal
result = String.Compare(str1, str2) == 0;
Console.WriteLine("Result of Compare Method: " +result);
// calling method
copymethod();
}
}
输出:
Result of Compare Method: True
Original Strings: str1 = 'GeeksforGeeks' and str2 ='geeks'
After Copy method
Strings are str1 = 'GeeksforGeeks' and str2='GeeksforGeeks'
运营商
Operator | Description |
---|---|
Equality(String, String) | Determines whether two specified strings have the same value. |
Inequality(String, String) | Determines whether two specified strings have different values. |
例子:
// C# program to illustrate the
// Equality and Inequality operator
using System;
class GFG {
// Main Method
public static void Main(string[] args)
{
string s1 = "WelcomeToGeeks";
string s2 = "WelcomeToGeeks";
bool result1, result2;
// Equality operator return true
// as both string are equal
result1 = s1 == s2;
// Inequality operator return false
// as both string are equal
result2 = s1 != s2;
Console.WriteLine(result1);
Console.WriteLine(result2);
}
}
输出:
True
False
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system。字符串?view = netframework-4.7.2#definition