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#串操作。

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.

【编辑推荐】

  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-11 14:20:41

C# .NET学习经验

2009-08-24 14:56:01

C#连接Access

2009-08-21 17:42:36

C#调用API

2009-09-02 14:14:44

C# COM接口转换

2009-09-01 13:10:39

C#读取Word

2009-09-01 13:00:05

C#实现Windows

2009-08-07 09:47:17

C#枚举C#数组

2009-08-27 15:45:30

C#正则表达式

2009-09-08 10:57:55

LINQ查询操作

2009-09-11 13:29:31

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技术栈公众号