C#数组和串操作经验总结

开发 后端
这里介绍C#数组和C#串操作,包括介绍C#中的串具有恒定不变的特性,即 一旦被创建,就不能改变长度或者改变其中任何的字符等。

C#数组有很多值得学习的地方,这里我们主要介绍存放字符序列的C#数组,包括介绍C#串操作等方面

关于C#数组和C#串操作:
1)串是由连续存储的字符组成
2)C#中的串具有恒定不变的特性,即 一旦被创建,就不能改变长度或者改变其中任何的字符。
3)串的连接、插入和删除等操作都是生成了新串而没有改变原串。
4)继承自 System.object。所以是引用类型(int,bool,char 等都是struct 不是class,是值类型)。
5)System.String 是密封类,所以不能被继承。
6)虽然System.String 是引用类型,但C#中将String 看作是基元类型,所以不用 new操作符创建实例,而是使用字符串驻留的机制。
7)System.String 继承自 IComparable, ICloneable, IConvertible, IComparable, IEnumerable, IEnumerable, IEquatable
8)C#提供了StringBuilder类型来支持高效地动态创建字符串。

下面是自定义一个string类,类中包含一个字段,用以存放字符序列的C#数组,还有一些常用的C#串操作。

  1. public class StringDS  
  2. {  
  3. private char[] data;//char数组  
  4. //索引器  
  5. public char this[int index]  
  6. {  
  7. get   
  8. {  
  9. return data[index];  
  10. }  
  11. set  
  12. {  
  13. data[index] = value;  
  14. }  
  15. }  
  16. //构造函数  
  17. public StringDS(char[] arr)  
  18. {  
  19. data = new char[arr.Length];  
  20. for (int i = 0; i < arr.Length; i++)  
  21. {  
  22. data[i] = arr[i];  
  23. }  
  24. }  
  25. //构造函数   
  26. public StringDS(int len)  
  27. {  
  28. char[] arr = new char[len];  
  29. data = arr;  
  30. }  
  31. //求串长   
  32. public int GetLength()  
  33. {  
  34. return data.Length;  
  35. }   
  36. //串比较   
  37. public int Compare(StringDS s)   
  38. {  
  39. int len=((this.GetLength()<=s.GetLength())?   
  40. this.GetLength():s.GetLength());   
  41. int i = 0;   
  42. for (i = 0; i < len; ++i)   
  43. {   
  44. if (this[i] != s[i])   
  45. {   
  46. break;   
  47. }   
  48. }   
  49. if (i <= len)  
  50. {  
  51. if (this[i] < s[i])  
  52. {  
  53. return -1;  
  54. }  
  55. else if (this[i] > s[i])  
  56. {  
  57. return 1;  
  58. }  
  59. }  
  60. else if (this.GetLength() == s.GetLength())  
  61. {  
  62. return 0;  
  63. }  
  64. else if (this.GetLength() < s.GetLength())  
  65. {  
  66. return -1;  
  67. }  
  68.  
  69. return 1;  
  70. }   
  71. //求子串   
  72. public StringDS SubString(int index, int len)   
  73. {   
  74. if ((index<0) || (index>this.GetLength()-1) || (len<0) || (len>this.GetLength()-index))   
  75. {   
  76. Console.WriteLine("Position or Length is error!");   
  77. return null;   
  78. }   
  79.  
  80. StringDS s = new StringDS(len);   
  81.  
  82. for (int i = 0; i < len; ++i)   
  83. {   
  84. s[i] = this[i + index-1];   
  85. }   
  86.  
  87. return s;   
  88. }   
  89. //串连接   
  90. public StringDS Concat(StringDS s)  
  91. {  
  92. StringDS s1 = new StringDS(this.GetLength() +s.GetLength());  
  93. for (int i = 0; i < this.GetLength(); ++i)  
  94. {  
  95. s1.data[i] = this[i];  
  96. }  
  97.  
  98. for (int j = 0; j < s.GetLength(); ++j)  
  99. {  
  100. s1.data[this.GetLength() + j] = s[j];  
  101. }  
  102.  
  103. return s1;  
  104. }   
  105. //串插入   
  106. public StringDS Insert(int index, StringDS s)   
  107. {   
  108. int len = s.GetLength();   
  109. int lenlen2 = len + this.GetLength();   
  110. StringDS s1 = new StringDS(len2);   
  111. if (index < 0 || index > this.GetLength() - 1)   
  112. {   
  113. Console.WriteLine("Position is error!");   
  114. return null;   
  115. }   
  116. for (int i = 0; i < index; ++i)   
  117. {   
  118. s1[i] = this[i];   
  119. }   
  120. for(int i = index; i < index + len ; ++i)   
  121. {   
  122. s1[i] = s[i - index];   
  123. }   
  124. for (int i = index + len; i < len2; ++i)   
  125. {   
  126. s1[i] = this[i - len];   
  127. }  
  128. return s1;  
  129. }  
  130. //串删除   
  131. public StringDS Delete(int index, int len)  
  132. {  
  133. if ((index < 0) || (index > this.GetLength() - 1)  
  134. || (len < 0) || (len > this.GetLength() - index))  
  135. {  
  136. Console.WriteLine("Position or Length is error!");  
  137. return null;  
  138. }  
  139.  
  140. StringDS s = new StringDS(this.GetLength() - len);  
  141.  
  142. for (int i = 0; i < index; ++i)  
  143. {  
  144. s[i] = this[i];  
  145. }  
  146.  
  147. for (int i = index + len; i < this.GetLength(); ++i)  
  148. {  
  149. s[i] = this[i];  
  150. }  
  151.  
  152. return s;  
  153. }   
  154. //串定位   
  155. public int Index(StringDS s)   
  156. {   
  157. if (this.GetLength() < s.GetLength())   
  158. {   
  159. Console.WriteLine("There is not string s!");   
  160. return -1;   
  161. }   
  162.    
  163. int i = 0;   
  164. int len = this.GetLength() - s.GetLength();   
  165. while (i < len)   
  166. {   
  167. if (this.Compare(s) == 0)   
  168. {  
  169. break;  
  170. }  
  171. }  
  172.  
  173. if (i <= len)  
  174. {  
  175. return i;  
  176. }  
  177.  
  178. return -1;  
  179. }   

【编辑推荐】

  1. C#数组基础介绍与操作详解
  2. C#数组初始化全面分析
  3. C#一维数组和多维数组浅谈
  4. C#参差数组初始化概述
  5. C#动态数组实例介绍
责任编辑:佚名 来源: 新浪科技
相关推荐

2009-08-27 11:21:36

C# override

2009-08-13 18:13:27

C#学习经验

2009-08-24 14:56:01

C#连接Access

2009-08-11 14:20:41

C# .NET学习经验

2009-08-21 17:42:36

C#调用API

2009-09-01 13:10:39

C#读取Word

2009-09-02 14:14:44

C# COM接口转换

2009-08-07 09:47:17

C#枚举C#数组

2009-09-01 13:00:05

C#实现Windows

2009-08-27 15:45:30

C#正则表达式

2009-09-11 13:29:31

LINQ查询操作

2009-09-08 10:57:55

LINQ查询操作

2009-08-26 15:39:08

C#隐式类型局部变量

2009-08-20 17:35:47

Servlet和JSP

2010-02-01 14:33:05

C++实现RTTI

2009-09-03 13:48:20

C#实现Web服务器功

2009-10-15 09:27:00

2010-01-21 14:49:44

VB.NET操作Wor

2010-05-06 15:04:54

Oracle建立DBL

2010-02-02 15:44:18

C++遍历集合
点赞
收藏

51CTO技术栈公众号