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

旌德網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司于2013年開(kāi)始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
關(guān)于C#數(shù)組和C#串操作:
1)串是由連續(xù)存儲(chǔ)的字符組成
2)C#中的串具有恒定不變的特性,即 一旦被創(chuàng)建,就不能改變長(zhǎng)度或者改變其中任何的字符。
3)串的連接、插入和刪除等操作都是生成了新串而沒(méi)有改變?cè)?br />4)繼承自 System.object。所以是引用類(lèi)型(int,bool,char 等都是struct 不是class,是值類(lèi)型)。
5)System.String 是密封類(lèi),所以不能被繼承。
6)雖然System.String 是引用類(lèi)型,但C#中將String 看作是基元類(lèi)型,所以不用 new操作符創(chuàng)建實(shí)例,而是使用字符串駐留的機(jī)制。
7)System.String 繼承自 IComparable, ICloneable, IConvertible, IComparable , IEnumerable , IEnumerable, IEquatable 。
8)C#提供了StringBuilder類(lèi)型來(lái)支持高效地動(dòng)態(tài)創(chuàng)建字符串。
下面是自定義一個(gè)string類(lèi),類(lèi)中包含一個(gè)字段,用以存放字符序列的C#數(shù)組,還有一些常用的C#串操作。
- public class StringDS
- {
- private char[] data;//char數(shù)組
- //索引器
- public char this[int index]
- {
- get
- {
- return data[index];
- }
- set
- {
- data[index] = value;
- }
- }
- //構(gòu)造函數(shù)
- public StringDS(char[] arr)
- {
- data = new char[arr.Length];
- for (int i = 0; i < arr.Length; i++)
- {
- data[i] = arr[i];
- }
- }
- //構(gòu)造函數(shù)
- public StringDS(int len)
- {
- char[] arr = new char[len];
- data = arr;
- }
- //求串長(zhǎng)
- public int GetLength()
- {
- return data.Length;
- }
- //串比較
- public int Compare(StringDS s)
- {
- int len=((this.GetLength()<=s.GetLength())?
- this.GetLength():s.GetLength());
- int i = 0;
- for (i = 0; i < len; ++i)
- {
- if (this[i] != s[i])
- {
- break;
- }
- }
- if (i <= len)
- {
- if (this[i] < s[i])
- {
- return -1;
- }
- else if (this[i] > s[i])
- {
- return 1;
- }
- }
- else if (this.GetLength() == s.GetLength())
- {
- return 0;
- }
- else if (this.GetLength() < s.GetLength())
- {
- return -1;
- }
- return 1;
- }
- //求子串
- public StringDS SubString(int index, int len)
- {
- if ((index<0) || (index>this.GetLength()-1) || (len<0) || (len>this.GetLength()-index))
- {
- Console.WriteLine("Position or Length is error!");
- return null;
- }
- StringDS s = new StringDS(len);
- for (int i = 0; i < len; ++i)
- {
- s[i] = this[i + index-1];
- }
- return s;
- }
- //串連接
- public StringDS Concat(StringDS s)
- {
- StringDS s1 = new StringDS(this.GetLength() +s.GetLength());
- for (int i = 0; i < this.GetLength(); ++i)
- {
- s1.data[i] = this[i];
- }
- for (int j = 0; j < s.GetLength(); ++j)
- {
- s1.data[this.GetLength() + j] = s[j];
- }
- return s1;
- }
- //串插入
- public StringDS Insert(int index, StringDS s)
- {
- int len = s.GetLength();
- int lenlen2 = len + this.GetLength();
- StringDS s1 = new StringDS(len2);
- if (index < 0 || index > this.GetLength() - 1)
- {
- Console.WriteLine("Position is error!");
- return null;
- }
- for (int i = 0; i < index; ++i)
- {
- s1[i] = this[i];
- }
- for(int i = index; i < index + len ; ++i)
- {
- s1[i] = s[i - index];
- }
- for (int i = index + len; i < len2; ++i)
- {
- s1[i] = this[i - len];
- }
- return s1;
- }
- //串刪除
- public StringDS Delete(int index, int len)
- {
- if ((index < 0) || (index > this.GetLength() - 1)
- || (len < 0) || (len > this.GetLength() - index))
- {
- Console.WriteLine("Position or Length is error!");
- return null;
- }
- StringDS s = new StringDS(this.GetLength() - len);
- for (int i = 0; i < index; ++i)
- {
- s[i] = this[i];
- }
- for (int i = index + len; i < this.GetLength(); ++i)
- {
- s[i] = this[i];
- }
- return s;
- }
- //串定位
- public int Index(StringDS s)
- {
- if (this.GetLength() < s.GetLength())
- {
- Console.WriteLine("There is not string s!");
- return -1;
- }
- int i = 0;
- int len = this.GetLength() - s.GetLength();
- while (i < len)
- {
- if (this.Compare(s) == 0)
- {
- break;
- }
- }
- if (i <= len)
- {
- return i;
- }
- return -1;
- }
- }
網(wǎng)站名稱(chēng):C#數(shù)組和串操作經(jīng)驗(yàn)總結(jié)
URL地址:http://www.fisionsoft.com.cn/article/djegoii.html


咨詢(xún)
建站咨詢(xún)
