自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#數(shù)組和串操作經(jīng)驗(yàn)總結(jié)

開發(fā) 后端
這里介紹C#數(shù)組和C#串操作,包括介紹C#中的串具有恒定不變的特性,即 一旦被創(chuàng)建,就不能改變長度或者改變其中任何的字符等。

C#數(shù)組有很多值得學(xué)習(xí)的地方,這里我們主要介紹存放字符序列的C#數(shù)組,包括介紹C#串操作等方面

關(guān)于C#數(shù)組和C#串操作:
1)串是由連續(xù)存儲的字符組成
2)C#中的串具有恒定不變的特性,即 一旦被創(chuàng)建,就不能改變長度或者改變其中任何的字符。
3)串的連接、插入和刪除等操作都是生成了新串而沒有改變原串。
4)繼承自 System.object。所以是引用類型(int,bool,char 等都是struct 不是class,是值類型)。
5)System.String 是密封類,所以不能被繼承。
6)雖然System.String 是引用類型,但C#中將String 看作是基元類型,所以不用 new操作符創(chuàng)建實(shí)例,而是使用字符串駐留的機(jī)制。
7)System.String 繼承自 IComparable, ICloneable, IConvertible, IComparable, IEnumerable, IEnumerable, IEquatable。
8)C#提供了StringBuilder類型來支持高效地動態(tài)創(chuàng)建字符串。

下面是自定義一個string類,類中包含一個字段,用以存放字符序列的C#數(shù)組,還有一些常用的C#串操作。

  1. public class StringDS  
  2. {  
  3. private char[] data;//char數(shù)組  
  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. //構(gòu)造函數(shù)  
  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. //構(gòu)造函數(shù)   
  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#數(shù)組基礎(chǔ)介紹與操作詳解
  2. C#數(shù)組初始化全面分析
  3. C#一維數(shù)組和多維數(shù)組淺談
  4. C#參差數(shù)組初始化概述
  5. C#動態(tài)數(shù)組實(shí)例介紹
責(zé)任編輯:佚名 來源: 新浪科技
相關(guān)推薦

2009-08-13 18:13:27

C#學(xué)習(xí)經(jīng)驗(yàn)

2009-08-27 11:21:36

C# override

2009-08-21 17:42:36

C#調(diào)用API

2009-08-11 14:20:41

C# .NET學(xué)習(xí)經(jīng)驗(yàn)

2009-08-24 14:56:01

C#連接Access

2009-09-01 13:10:39

C#讀取Word

2009-09-02 14:14:44

C# COM接口轉(zhuǎn)換

2009-09-01 13:00:05

C#實(shí)現(xiàn)Windows

2009-08-07 09:47:17

C#枚舉C#數(shù)組

2009-08-27 15:45:30

C#正則表達(dá)式

2009-09-08 10:57:55

LINQ查詢操作

2009-09-11 13:29:31

LINQ查詢操作

2009-08-26 15:39:08

C#隱式類型局部變量

2009-09-03 13:48:20

C#實(shí)現(xiàn)Web服務(wù)器功

2009-08-20 17:35:47

Servlet和JSP

2010-05-06 15:04:54

Oracle建立DBL

2010-02-01 14:33:05

C++實(shí)現(xiàn)RTTI

2009-10-15 09:27:00

2010-01-21 14:49:44

VB.NET操作Wor

2010-05-25 15:51:25

MySQL連接字符串
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號