String类的实现--StringDS

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值