using System;
using System.Collections.Generic;
using System.Text;
namespace 串
{
class StringDS
{
private char[] date;
public StringDS(char[] array )
{
date = new char[array.Length];
for (int i = 0; i < date.Length; i++)
{
date[i] = array[i];
}
}
public StringDS(string str)
{
date = new char[str.Length];
for (int i = 0; i < date.Length; i++)
{
date[i] = str[i];
}
}
//索引器
public char this[int index]
{
get { return date[index]; }
}
public int GetLength()
{
return date.Length;
}
/// <summary>
/// 如果两字符相同返回0
/// 如果当前字符小于s,返回-1
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
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;
}
}
}
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="index"></param>
/// <param name="length"></param>
/// <returns></returns>
public StringDS SubString(int index,int length)
{
char[] newDate = new char[length];
for (int i = 0; i < length; i++)
{
newDate[i] = date[i+index ];
}
return new StringDS ( newDate);
}
public override string ToString()
{
/*string s="";
foreach (char item in date)
{
s += item;
}
return s;*/
return new string(date);
}
public static StringDS Concat(StringDS a,StringDS b)
{
char [] newDate = new char[a.GetLength() + b.GetLength()];
for (int i = 0; i < a.GetLength (); i++)
{
newDate[i] = a[i];
}
for (int i = a.GetLength (); i < newDate .Length; i++)
{
newDate[i] = b[i - a.GetLength()];
}
return new StringDS(newDate);
}
public int IndexOf(StringDS str)
{
for (int i = 0; i <= this .GetLength ()-str .GetLength (); i++)
{
bool isEqual = true;
for (int j = i; j < i+str.GetLength (); j++)
{
if (this [j]!=str [j-i])
{
isEqual = false;
}
}
if (isEqual )
{
return i;
}else
{
continue;
}
}
return -1;
}
}
}