using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace 串
{
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 StringDS ()
{
data = null;
}
public char this[int i]
{
get{return data[i];}
}
public int GetLength()
{
return data.Length;
}
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)
{
return this[index] > s[index] ? 1 : -1;
}
else
{
if (this.GetLength()==s.GetLength())
{
return 0;
}
else
{
return this.GetLength() < s.GetLength() ? -1 : 1;
}
}
}
public StringDS SubString(int index,int length)
{
char[] newdata;
if (index < 0|| index > this.GetLength()-1||length<=0)
{
Console.WriteLine("The index does exsit!");
return default(StringDS);
}
else
{
if (index+length<=this.GetLength())
{
newdata = new char[length];
for (int i = index; i <index +length; i++)
{
newdata[i-index]= this.data[i];
}
return new StringDS(newdata );
}
else
{
newdata = new char[this.GetLength()-index ];
for (int i = index; i < this.GetLength(); i++)
{
newdata[i - index] = this.data[i];
}
return new StringDS(newdata);
}
}
}
public StringDS Concal(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 < newData.Length; i++)
{
newData[i] = s2[i-s1.GetLength()];
}
return new StringDS(newData);
}
public int IndexOf(StringDS s)
{
bool isEqual = true;
if (this.GetLength()<s.GetLength())
{
Console.WriteLine("当前串长度小于目标串");return -1;
}
else
{
for (int i = 0; i <= this.GetLength()-s.GetLength(); i++)
{
for (int j = 0; j < i+s.GetLength(); j++)
{
if (this[j]!=s[j-i])
{
isEqual = false;
}
}
if (isEqual)
{
return i;
}
}
}
return -1;
}
public void OutPut()
{
for (int i = 0; i < data.Length ; i++)
{
Console.Write(data[i]);
}
Console.WriteLine();
}
}
}