在应用程序中使用最频繁的类型是字符串,尽管C#中已经有了String类型,废话不多说了。
实现代码如下:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![]()
5
namespace 串和数组
6
{
7
class Program
8
{
9
public class StringDS
10
{
11
//字符数组
12
private char[] data;
13
//索引器
14
public char this[int index]
15
{
16
get
17
{
18
return data[index];
19
}
20
}
21
//构造器
22
public StringDS(char[] arr)
23
{
24
data=new char[arr.Length];
25
for (int i = 0; i < arr.Length; i++)
26
{
27
data[i] = arr[i];
28
}
29
}
30
//构造器
31
public StringDS(StringDS s)
32
{
33
for (int i = 0; i < s.data.Length; i++)
34
{
35
data[i] = s.data[i];
36
}
37
}
38
//构造器
39
public StringDS(int len)
40
{
41
char[] arr=new char[len];
42
data = arr;
43
}
44
//求串长
45
public int GetLength()
46
{
47
return data.Length;
48
}
49
//串比较
50
public int Compare(StringDS s)
51
{
52
int len = ((this.GetLength() <= s.GetLength()) ? this.GetLength() : s.GetLength());
53
int i = 0;
54
for (i = 0; i < len; i++)
55
{
56
if (this[i] != s.data[i])
57
{
58
break;
59
}
60
}
61
if (i < len)
62
{
63
if (this[i] < s.data[i])
64
{
65
return -1;
66
}
67
else if (this[i] > s.data[i])
68
{
69
return 1;
70
}
71
}
72
else if(this.GetLength()==s.GetLength())
73
{
74
return 0;
75
}
76
else if (this.GetLength() < s.GetLength())
77
{
78
return -1;
79
}
80
return 1;
81
}
82![]()
83
//求子串
84
public StringDS SubString(int index, int len)
85
{
86
if ((index < 0) || (index > this.GetLength() - 1) || (len < 0) || (len > this.GetLength() - index))
87
{
88
Console.WriteLine("位置错误!");
89
return null;
90
}
91
StringDS s = new StringDS(len);
92
for (int i = 0; i < len; ++i)
93
{
94
s.data[i]=this[i+index-1];
95
}
96
return s;
97![]()
98
}
99
//串连接
100
public StringDS Concat(StringDS s)
101
{
102
StringDS s1 = new StringDS(this.GetLength()+s.GetLength());
103
for (int i = 0; i < this.GetLength(); ++i)
104
{
105
s1.data[i]=this[i];
106
}
107
for (int j = 0; j < s.GetLength(); j++)
108
{
109
110
s1.data[this.GetLength()+j]=s[j];
111
112
}
113
return s1;
114
}
115
//串插入
116
public StringDS Insert(int index, StringDS s)
117
{
118
int len = s.GetLength();
119
int len2 = len + this.GetLength();
120
StringDS s1 = new StringDS(len2);
121
if (index < 0 || index > this.GetLength() - 1)
122
{
123
Console.WriteLine("位置错误!");
124
return null;
125
}
126
for (int i = 0; i < index ; ++i)
127
{
128
s1.data[i] = this[i];
129
}
130
for (int i = index; i < index + len; ++i)
131
{
132
s1.data[i] = s[i - index];
133
}
134
for (int i = index + len; i < len2; i++)
135
{
136
s1.data[i] = this[i - len];
137
}
138
return s1;
139
}
140
//串删除
141
public StringDS Delete(int index,int len)
142
{
143
if ((index < 0) || (index > this.GetLength() - 1) || (len < 0) || (len > this.GetLength() - index))
144
{
145
Console.WriteLine("位置错误!");
146
return null;
147
}
148
StringDS s = new StringDS(this.GetLength()-len);
149
for (int i = 0; i < index; ++i)
150
{
151
s.data[i] = this[i];
152
}
153
for (int i = index + len; i < this.GetLength(); ++i)
154
{
155
s.data[i] = this[i];
156
}
157
return s;
158
}
159
//串定位
160
public int Index(StringDS s)
161
{
162
if (this.GetLength() < s.GetLength())
163
{
164
Console.WriteLine("这里没有"+s+"");
165
return -1;
166![]()
167
}
168
int i = 0;
169
int len = this.GetLength() - s.GetLength();
170
while(i<len)
171
{
172
if (this.Compare(s) == 0)
173
{
174
break;
175
}
176![]()
177
}
178![]()
179
if(i<=len)
180
{
181
return i;
182
}
183
return -1;
184
}
185![]()
186
}
187
static void Main(string[] args)
188
{
189
char[] f = new char[] {'a','b','f','s'};
190
char[] m = new char[] {'d','v','e','k'};
191
StringDS str1 = new StringDS(f);
192
StringDS str2 = new StringDS(m);
193
str1.Insert(3, str2);
194
Console.WriteLine(str1.Index(str2));
195
str1.Delete(3,1);
196
197
}
198
}
199
}
200![]()
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace 串和数组6
{7
class Program8
{9
public class StringDS10
{ 11
//字符数组12
private char[] data;13
//索引器14
public char this[int index]15
{16
get 17
{18
return data[index];19
}20
}21
//构造器22
public StringDS(char[] arr)23
{24
data=new char[arr.Length];25
for (int i = 0; i < arr.Length; i++)26
{27
data[i] = arr[i];28
}29
}30
//构造器31
public StringDS(StringDS s)32
{33
for (int i = 0; i < s.data.Length; i++)34
{35
data[i] = s.data[i];36
}37
}38
//构造器39
public StringDS(int len)40
{ 41
char[] arr=new char[len];42
data = arr;43
}44
//求串长45
public int GetLength()46
{47
return data.Length;48
}49
//串比较50
public int Compare(StringDS s)51
{52
int len = ((this.GetLength() <= s.GetLength()) ? this.GetLength() : s.GetLength());53
int i = 0;54
for (i = 0; i < len; i++)55
{56
if (this[i] != s.data[i])57
{58
break;59
}60
}61
if (i < len)62
{63
if (this[i] < s.data[i])64
{65
return -1;66
}67
else if (this[i] > s.data[i])68
{69
return 1;70
}71
}72
else if(this.GetLength()==s.GetLength())73
{74
return 0;75
}76
else if (this.GetLength() < s.GetLength())77
{78
return -1;79
}80
return 1;81
}82

83
//求子串84
public StringDS SubString(int index, int len)85
{86
if ((index < 0) || (index > this.GetLength() - 1) || (len < 0) || (len > this.GetLength() - index))87
{88
Console.WriteLine("位置错误!");89
return null;90
}91
StringDS s = new StringDS(len);92
for (int i = 0; i < len; ++i)93
{ 94
s.data[i]=this[i+index-1];95
}96
return s;97

98
}99
//串连接100
public StringDS Concat(StringDS s)101
{102
StringDS s1 = new StringDS(this.GetLength()+s.GetLength());103
for (int i = 0; i < this.GetLength(); ++i)104
{ 105
s1.data[i]=this[i];106
}107
for (int j = 0; j < s.GetLength(); j++)108
{ 109
110
s1.data[this.GetLength()+j]=s[j];111
112
}113
return s1;114
}115
//串插入116
public StringDS Insert(int index, StringDS s)117
{118
int len = s.GetLength();119
int len2 = len + this.GetLength();120
StringDS s1 = new StringDS(len2);121
if (index < 0 || index > this.GetLength() - 1)122
{123
Console.WriteLine("位置错误!");124
return null;125
}126
for (int i = 0; i < index ; ++i)127
{128
s1.data[i] = this[i];129
}130
for (int i = index; i < index + len; ++i)131
{132
s1.data[i] = s[i - index];133
}134
for (int i = index + len; i < len2; i++)135
{136
s1.data[i] = this[i - len];137
}138
return s1;139
}140
//串删除141
public StringDS Delete(int index,int len)142
{143
if ((index < 0) || (index > this.GetLength() - 1) || (len < 0) || (len > this.GetLength() - index))144
{145
Console.WriteLine("位置错误!");146
return null;147
}148
StringDS s = new StringDS(this.GetLength()-len);149
for (int i = 0; i < index; ++i)150
{151
s.data[i] = this[i];152
}153
for (int i = index + len; i < this.GetLength(); ++i)154
{155
s.data[i] = this[i];156
}157
return s;158
}159
//串定位160
public int Index(StringDS s)161
{162
if (this.GetLength() < s.GetLength())163
{164
Console.WriteLine("这里没有"+s+"");165
return -1;166

167
}168
int i = 0;169
int len = this.GetLength() - s.GetLength();170
while(i<len)171
{172
if (this.Compare(s) == 0)173
{174
break;175
}176

177
}178

179
if(i<=len)180
{181
return i;182
}183
return -1;184
}185

186
}187
static void Main(string[] args)188
{189
char[] f = new char[] {'a','b','f','s'};190
char[] m = new char[] {'d','v','e','k'};191
StringDS str1 = new StringDS(f);192
StringDS str2 = new StringDS(m);193
str1.Insert(3, str2);194
Console.WriteLine(str1.Index(str2));195
str1.Delete(3,1);196
197
}198
}199
}200



浙公网安备 33010602011771号