C#字符串类String用法详细介绍

2021年3月17日18:26:40 发表评论 636 次浏览

在C#中, String是Unicode字符序列或字符数组。

Unicode字符的范围将是U+0000到U+FFFF。字符数组也称为文本。所以字符串是文本的表示。字符串由类System.String表示。

String类在. net基类库中定义。换句话说,字符串对象是System的顺序集合。表示字符串的Char对象。字符串对象在内存中的最大大小可以是2GB或约10亿字符。

字符串类的特征:

  • 的System.String类是不可变的, 即一旦创建, 其状态就无法更改。
  • 借助length属性, 它提供了给定字符串中存在的字符总数。
  • 字符串对象可以包含一个空字符, 该字符将算作字符串长度的一部分。
  • 它提供了给定字符串中字符的位置。
  • 它允许空字符串。空字符串是包含零个字符的String对象的有效实例。
  • 已声明但尚未分配值的字符串为null。尝试在该字符串上调用方法将引发NullReferenceException.
  • 它还支持搜索字符串, 字符串比较, 相等性测试, 修改字符串, 字符串规范化, 字符串复制等。
  • 它还提供了几种创建字符串的方式, 例如使用构造函数, 使用串联等。

构造器

构造器 描述
String(字符*) 将String类的新实例初始化为由指向Unicode字符数组的指定指针指示的值。
String(Char *, Int32, Int32) 将String类的新实例初始化为由指向Unicode字符数组的指定指针, 该数组内的起始字符位置以及长度所指示的值。
String(Char, Int32) 将String类的新实例初始化为指定的Unicode字符重复指定次数的值。
String(Char[]) 将String类的新实例初始化为Unicode字符数组指示的值。
String(Char [], Int32, Int32) 将String类的新实例初始化为Unicode字符数组, 该数组内的起始字符位置以及长度所指示的值。
String(SByte *) 将String类的新实例初始化为指向8位带符号整数数组的指针所指示的值。
String(SByte *, Int32, Int32) 将String类的新实例初始化为由指向8位带符号整数数组, 该数组内的起始位置和长度的指定指针所指示的值。
String(SByte *, Int32, Int32, Encoding) 将String类的新实例初始化为由指向8位带符号整数的数组的指定指针, 该数组内的起始位置, 长度和Encoding对象所指定的值。

例子:

// 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

属性

属性 描述
Chars[Int32] 获取当前String对象中指定位置的Char对象。
Length 获取当前String对象中的字符数。

例子:

// C# program to demonstrate the
// String Class Properties
using System;
  
class Geeks {
   
     // Main Method
     public static void Main()
     {
         string str = "lsbin" ;
          
         // 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

方法

方法 描述
Clone() 返回对此String实例的引用。
Compare() 用于比较两个字符串对象。
CompareOrdinal(String, Int32, String, Int32, Int32) 通过评估每个子字符串中对应的Char对象的数值, 比较两个指定的String对象的子字符串。
CompareOrdinal(String, String) 通过评估每个字符串中相应的Char对象的数值, 比较两个指定的String对象。
CompareTo() 将当前实例与指定的Object或String对象进行比较。
Concat() 连接一个或多个String实例, 或一个或多个Object实例的值的String表示形式。
Contains(字符串) 返回一个值, 该值指示在该字符串内是否出现指定的子字符串。
Copy(字符串) 创建一个新的String实例, 该实例的值与指定的String相同。
CopyTo(Int32, Char [], Int32, Int32) 将指定数量的字符从此实例中的指定位置复制到Unicode字符数组中的指定位置。
EndsWith() 确定此字符串实例的末尾是否与指定的字符串匹配。
Equals() 确定两个String对象是否具有相同的值。
Format() 根据指定的格式将对象的值转换为字符串, 并将其插入另一个字符串。
GetEnumerator() 检索可以迭代此字符串中各个字符的对象。
GetHashCode() 返回此字符串的哈希码。
GetType() 获取当前实例的类型。
(继承自Object)
GetTypeCode() 返回String类的TypeCode。
IndexOf() 报告此实例中首次出现指定的Unicode字符或字符串的从零开始的索引。如果在此实例中找不到字符或字符串, 则该方法返回-1。
IndexOfAny() 报告指定实例的Unicode字符数组中任何字符在此实例中首次出现的索引。如果在此实例中找不到数组中的字符, 则该方法返回-1。
Insert(Int32, String) 返回一个新字符串, 在此实例中, 在指定的索引位置插入了指定的字符串。
Intern(String) 检索系统对指定字符串的引用。
IsInterned(String) 检索对指定String的引用。
IsNormalized() 指示此字符串是否采用特定的Unicode规范化形式。
IsNullOrEmpty(String) 指示指定的字符串是null还是Empty字符串。
IsNullOrWhiteSpace(String) 指示指定的字符串是null, 空还是仅由空格字符组成。
Join() 使用每个元素或成员之间的指定分隔符, 连接指定数组的元素或集合的成员。
LastIndexOf() 报告此实例中最后一次出现指定的Unicode字符或字符串的从零开始的索引位置。如果在此实例中找不到字符或字符串, 则该方法返回-1。
MemberwiseClone() 创建当前对象的浅表副本。
(继承自Object)
Normalize() 返回一个新的字符串, 其二进制表示形式采用特定的Unicode规范化形式。
PadLeft() 返回指定长度的新字符串, 其中当前字符串的开头用空格或指定的Unicode字符填充。
PadRight() 返回指定长度的新字符串, 其中当前字符串的末尾用空格或指定的Unicode字符填充。
Remove() 返回一个新字符串, 从当前字符串中删除指定数量的字符。
Replace() 返回一个新字符串, 其中当前字符串中所有出现的指定Unicode字符或字符串都替换为另一个指定的Unicode字符或字符串。
Split() 返回一个字符串数组, 其中包含此实例中的子字符串, 这些子字符串由指定的字符串或Unicode字符数组的元素分隔。
StartsWith(String) 确定此字符串实例的开头是否与指定的字符串匹配。
Substring(Int32) 从此实例检索子字符串。
ToCharArray() 将此实例中的字符复制到Unicode字符数组。
ToLower() 返回此字符串的副本, 转换为小写形式。
ToLowerInvariant() 返回此String对象的副本, 该副本使用不变区域性的大小写规则转换为小写。
ToString() 将此实例的值转换为String。
ToUpper() 返回此字符串的副本, 转换为大写形式。
ToUpperInvariant() 返回使用不变文化的大小写规则将此String对象转换为大写形式的副本。
Trim() 返回一个新字符串, 其中删除了当前String对象中一组指定字符的所有开头和结尾。
TrimEnd(Char []) 从当前String对象中删除数组中指定的一组字符的所有结尾出现。
TrimStart(Char []) 从当前String对象中删除数组中指定的一组字符的所有前导出现。

例子:

// C# program to illustrate
// String class methods
using System;
   
class GFG {
      
     static void copymethod()
     {
         string str1 = "lsbin" ;
         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 = "lsbin" ;
         string str2 = "lsbin" ;
   
         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 = 'lsbin' and str2 ='geeks'

After Copy method

Strings are str1 = 'lsbin' and str2='lsbin'

操作符

操作符 描述
Equality(String, String) 确定两个指定的字符串是否具有相同的值。
Inequality(String, String) 确定两个指定的字符串是否具有不同的值。

例子:

// 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.string?view=netframework-4.7.2#definition

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: