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
8)C#提供了StringBuilder类型来支持高效地动态创建字符串。
下面是自定义一个string类,类中包含一个字段,用以存放字符序列的C#数组,还有一些常用的C#串操作。
public class StringDS
{
private char[] data;//char数组
//索引器
public char this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
//构造函数
public StringDS(char[] arr)
{
data = new char[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
data[i] = arr[i];
}
}
//构造函数
public StringDS(int len)
{
char[] arr = new char[len];
data = arr;
}
//求串长
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;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
【编辑推荐】