public class stringDS
{
private char[] data; //用来存放字符串中的字符
public stringDS(char[] array)
{
data=new char[array.Length];
for (int i = 0; i < data.Length; i++)
{
data[i] = array[i];
}
}
public stringDS(string str)
{
data=new char[str.Length];
for (int i = 0; i < data.Length; i++)
{
data[i] = str[i];
}
}
public int GetLength()
{
return data.Length;
}
public char this[int index]
{
get { return data[index]; }
}
//如果当前字符串和s相等,返回0
//如果当前字符串大于s,返回1
//如果当前字符串小于s,返回-1
public int Compare(stringDS s)
{
int len = this.GetLength() < s.GetLength() ? this.GetLength() : s.GetLength();//取得两个字符串中,长度更小的字符串长度
int index = -1; //存储不想等的字符的索引位置
for (int i = 0; i < len; i++)
{
if (this[i] != s[i]) //找到不想等的字符
{
index = i;
break;
}
}
if (index != -1) //值不想等,判断谁大
{
if (this[index] > s[index])
{
return 1;
}
else
{
return -1;
}
}
else
{
if (this.GetLength() == s.GetLength())
{
return 0;
}
else if(this.GetLength()>s.GetLength())
{
return 1;
}
else
{
return -1;
}
}
}
public stringDS SubString(int index,int length) //获取子字符串
{
char[] newdata=new char[length];
for (int i = index; i < index + length; i++)
{
newdata[i - index] = data[i];
}
return new stringDS(newdata);
}
public stringDS Append(stringDS s1, stringDS s2)
{
char[] newdata=new char[s1.GetLength()+s2.GetLength()];
for (int i = 0; i < s1.GetLength(); i++)
{
newdata[i] = s1[i];
}
for (int i = s1.GetLength(); i < s1.GetLength() + s2.GetLength(); i++)
{
newdata[i] = s2[i - s1.GetLength()];
}
return new stringDS(newdata);
}
public int IndexOf(stringDS s)
{
for (int i = 0; i <= this.GetLength() - s.GetLength(); i++)
{
bool res = true;
for (int j = i; j < i + s.GetLength(); i++)
{
if (this[j] != s[j - i])
{
res = false;
}
}
if (res)
{
return i;
}
else
{
continue;
}
}
return -1;
}
}