> 文章列表 > 08 C# 字符串

08 C# 字符串

08 C# 字符串

C# 中什么是串

在C# 中,"串"是指字符序列,也就是一组按照特定顺序排列的字符。在C中,使用字符串类型表示一个串。字符串类型是一个引用类型,用于表示一组字符,通常使用双引号或单引号来定义。

例如:

string str1 = "Hello World!"; // 使用双引号定义字符串
string str2 = 'A'; // 使用单引号定义字符串

字符串在C# 中有非常多的常见操作,包括连接、比较、查找、替换等,它可以使用多种方法进行操作。在C中执行大量字符串操作时,使用 StringBuilder 类会比直接操作字符串要更高效。

用表格总结C# 中串的特点

特点 描述
不可变性 字符串对象一旦创建,其内容就不能被更改
字符类型 C# 中的字符串是由字符组成的 Unicode 序列
索引 可以通过索引值访问字符串中的单个字符
可迭代性 可以使用 foreach 循环来迭代访问字符串中的每个字符
长度属性 可以使用 Length 属性获取字符串中的字符数
拼接操作 可以使用加号(+)实现字符串的拼接操作
格式化字符串 可以使用 $ 符号或者 string.Format 方法实现字符串的格式化操作
转义字符 可以使用反斜杠(\\)作为转义字符,表示一些特殊的字符或者字符序列
字符串比较 可以使用 == 或者 Equals 方法进行字符串的比较操作
字符串搜索 可以使用 IndexOf 和 LastIndexOf 方法在字符串中搜索指定的子串
子串提取 可以使用 Substring 方法从字符串中提取指定位置和长度的子串
字符串分割 可以使用 Split 方法将字符串按照指定的分隔符进行分割

构造器

   private char[] data;//Used to store the characters in a stringpublic StringDS(char[]array){data = new char[array.Length];for (int i = 0; i < data.Length; i++){data[i] = array[i];}}//Decompose the incoming string into a private stringpublic StringDS(string str){data = new char[str.Length];for (int i = 0; i < data.Length; i++){data[i] = str[i];}}

索引器

一般字符串需要一个索引器,通过索引器去访问字符串的单个字符

 //根据索引访问字符的索引器/Indexer for accessing characters by indexpublic char this[int index]{get {return data[index];}}
//获取索引器的长度Get the length of the indexerpublic int GetLength(){return data.Length;}

Compare()方法

可以做排序,比较,返回int

 /// <summary>/// 如果两个字符串一样,那么返回0/If the two strings are the same,return 0/// 如果当前字符串小于s,那么返回-1/If the current string is smaller than s ,then -1 is returned/// 如果当前字符串大于s,那么返回1 ,比较的是不是相等,不等,比较ASCII大小/If the current string is biggger than s,then 1 is return/// </summary>/// <param name="s"></param>/// <returns></returns>public int Compare(StringDS s){//取得两个字符串中,长度更小的字符串/get the smaller of two stringsint len = this.GetLength() < s.GetLength() ? this.GetLength() : s.GetLength();int index = -1;//存储不相等的字符的索引位置/Store the index position of unequal charactersfor (int i = 0; i < len; i++){if (this[i] != s[i]){index = i;break;}}//for循环里面有一次满足了比较时,两个字符不一样的情况if (index!=-1){if (this[index] > s[index]){return 1;}else{return -1;}}else{if (this.GetLength()==s.GetLength()){return 0;}else{if (this.GetLength()> s.GetLength()){return 1;}else{return -1;}}}}

求子字符串

  //求得一个字符串的子字符串public StringDS SubString(int index,int length){char[] newData = new char[length];for (int i = index ; i < index+length; i++){newData[i - index] = data[i];}return new StringDS(newData);}

拼接字符串

  //拼接public static StringDS Concat(StringDS s1, StringDS s2){//s1 0-s1.leng-1char[] newData = new char[s1.GetLength() + s2.GetLength()];for (int i = 0; i < s1.GetLength(); i++){newData[i] = s1[i];}for (int i = s1.GetLength(); i < newData.Length; i++){newData[i] = s2[i - s1.GetLength()];}return new StringDS(newData);}

子字符串索引位置

   //s是子字符串长度/// <summary>/// 从哪一个索引开始是含子字符串/// </summary>/// <param name="s"></param>/// <returns></returns>public int IndexOf(StringDS s){for (int i = 0; i <= this.GetLength()-s.GetLength(); i++){bool isEqual = true;for (int j = i; j < i+s.GetLength(); j++){if (this[j]!=s[j-i]){isEqual = false;}}if (isEqual){return i;}else{continue;}}return -1;}

这个比较难理解,

比如当前字符串this​ 是12345​,this.GetLength()​长度为5​。s字符串是234​,s.GetLength()​为3​。两个长度差是2​,i在第一层循环,j​是this​的索引,i每次增加1​,j都从i开始,即j每次移动一个索引开始遍历,而且遍历长度是一定的,都为s​长度。而i遍历的长度为两个字符串长度差,是因为j​遍历长度总为s.GetLength()​,当i​移动到差值这个值的时候,j的最后一位索引为即this.GetLength()−s.GetLength()+s.GetLength()=this.GetLength()this.GetLength()-s.GetLength()+s.GetLength()=this.GetLength()this.GetLength()s.GetLength()+s.GetLength()=this.GetLength();

哈萨克斯坦旅游网