400-028-6601
建站资讯

网站建设资讯

为你提供网站建设行业资讯、网站优化知识、主机域名邮箱、网站开发常见问题等。

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

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

旌德网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联公司于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。

关于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. return 1;
  69. } 
  70. //求子串 
  71. public StringDS SubString(int index, int len) 
  72. { 
  73. if ((index<0) || (index>this.GetLength()-1) || (len<0) || (len>this.GetLength()-index)) 
  74. { 
  75. Console.WriteLine("Position or Length is error!"); 
  76. return null; 
  77. } 
  78. StringDS s = new StringDS(len); 
  79. for (int i = 0; i < len; ++i) 
  80. { 
  81. s[i] = this[i + index-1]; 
  82. } 
  83. return s; 
  84. } 
  85. //串连接 
  86. public StringDS Concat(StringDS s)
  87. {
  88. StringDS s1 = new StringDS(this.GetLength() +s.GetLength());
  89. for (int i = 0; i < this.GetLength(); ++i)
  90. {
  91. s1.data[i] = this[i];
  92. }
  93. for (int j = 0; j < s.GetLength(); ++j)
  94. {
  95. s1.data[this.GetLength() + j] = s[j];
  96. }
  97. return s1;
  98. } 
  99. //串插入 
  100. public StringDS Insert(int index, StringDS s) 
  101. { 
  102. int len = s.GetLength(); 
  103. int lenlen2 = len + this.GetLength(); 
  104. StringDS s1 = new StringDS(len2); 
  105. if (index < 0 || index > this.GetLength() - 1) 
  106. { 
  107. Console.WriteLine("Position is error!"); 
  108. return null; 
  109. } 
  110. for (int i = 0; i < index; ++i) 
  111. { 
  112. s1[i] = this[i]; 
  113. } 
  114. for(int i = index; i < index + len ; ++i) 
  115. { 
  116. s1[i] = s[i - index]; 
  117. } 
  118. for (int i = index + len; i < len2; ++i) 
  119. { 
  120. s1[i] = this[i - len]; 
  121. }
  122. return s1;
  123. }
  124. //串删除 
  125. public StringDS Delete(int index, int len)
  126. {
  127. if ((index < 0) || (index > this.GetLength() - 1)
  128. || (len < 0) || (len > this.GetLength() - index))
  129. {
  130. Console.WriteLine("Position or Length is error!");
  131. return null;
  132. }
  133. StringDS s = new StringDS(this.GetLength() - len);
  134. for (int i = 0; i < index; ++i)
  135. {
  136. s[i] = this[i];
  137. }
  138. for (int i = index + len; i < this.GetLength(); ++i)
  139. {
  140. s[i] = this[i];
  141. }
  142. return s;
  143. } 
  144. //串定位 
  145. public int Index(StringDS s) 
  146. { 
  147. if (this.GetLength() < s.GetLength()) 
  148. { 
  149. Console.WriteLine("There is not string s!"); 
  150. return -1; 
  151. } 
  152.  
  153. int i = 0; 
  154. int len = this.GetLength() - s.GetLength(); 
  155. while (i < len) 
  156. { 
  157. if (this.Compare(s) == 0) 
  158. {
  159. break;
  160. }
  161. }
  162. if (i <= len)
  163. {
  164. return i;
  165. }
  166. return -1;
  167. } 
  168. }

当前标题:C#数组和串操作经验总结
当前路径:http://www.hnjierui.cn/article/djegoii.html

其他资讯