C#实现字符串类

 C#实现字符串类。学完之后对数据结构还有C#有了更深层次的理解。有正在学的的朋友,欢迎探讨。QQ:848367854
namespace _stringStudy
{
    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<str.Length; i++)
            {
                data[i] = str[i];
            }
        }

        public char this[int index]             //根据索引访问字符的索引器
        {
            get { return data[index]; }
        }

        public int GetLength()                  //取得字符串长度的方法
        {
            return data.Length;
        }
        /// <summary>
        ///  1.如果当前字符串等于s返回0
        ///  2.如果当前字符大于s,返回1
        ///  3.如果当前字符小于s,返回-1
        ///  4.如果当前字符串长度大s的长度,返回1,否则返回-1
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public int Compare(StringDS s)
        {
            int index = -1;                                     //索引
            int len = this.GetLength() < s.GetLength() ? this.GetLength() : s.GetLength();//三目运算符,找到长度小的字符串长度
            for (int i = 0; i < len; i++)
            {
                if (this[i] != s[i])                            //只要发现一处字符不同,把该字符索引值赋值给index
                {
                    index = i;
                    break;                                      //一经发现不同,立即跳出循环
                }              
            }
            if(index != -1)                                     //字符串前len个长度找到了不同的字符,并记录不同字符处索引值为 index
            {
                if (this[index]> s[index])                     //如果当前字符串第index位置的字符值 大于 s字符串第index位置的值, 返回1
                {
                    return 1;
                }
                else                                           //如果当前字符串第index位置的字符值 小于 s字符串第index位置的值, 返回-1
                {
                    return -1;
                }
            }
            else                                                //字符串前len个字符都相同,又分3种情况
            {
                if(this.GetLength() == s.GetLength())          // 情况1:当前字符串串长度等于s字符串长度,返回0
                {
                    return 0;
                }
                else
                {
                    if (this.GetLength() > s.GetLength())      // 情况2: 当前字符串长度大于s字符串, 返回 1
                    {
                        return 1;
                    }
                    else                                       // 情况3: 当前字符串长度小于s字符串, 返回-1
                    {
                        return -1;
                    }
                }
               

            }
        }
        public StringDS SubString(int index, int length)
        {
            char[] newChar = new char[length];
            for(int i = index; i < index + length; i++)
            {
                newChar[i - index] = data[i];
            }
            return new StringDS(newChar);   
        }
        public StringDS Concat(StringDS s1,StringDS s2)
        {
            char[] newChar = new char[s1.GetLength() + s2.GetLength()];
            for(int i = 0; i < s1.GetLength(); i++)
            {
                newChar[i] = s1[i];
            }
            for(int i = s1.GetLength(); i < newChar.Length; i++)
            {
                newChar[i] = s2[i - s1.GetLength()];
            }
            return new StringDS(newChar);
        }
        public int IndexOf(StringDS s)
        {
            for(int i = 0; i <=this.GetLength() - s.GetLength();i++)
            {
                bool isEqual = true;
                for (int j = i; j < i + s.GetLength(); j++)
                {
                    if(this[j] != s[j - i])
                    {
                        isEqual = false;
                    }
                }
                if (isEqual)
                {
                    return i;
                }
                else
                {
                    continue;
                }
            }
            return -1;
        }

    }

}



namespace _stringStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] array = new char[] { 'A','B' ,'C'};
            Console.WriteLine(new StringDS(array).ToString());     //ABC


            StringDS s1 = new StringDS("abc");
            StringDS s2 = new StringDS("c");
            int j = s1.IndexOf(s2);
            Console.WriteLine(j);                                  //2
            
            StringDS s3 = new StringDS("I am a girl");
            Console.WriteLine(s3.ToString());                     //I am a girl


            StringDS s4 = new StringDS("the great");
            StringDS s5 = new StringDS(" wall");


            Console.WriteLine(s3.GetLength());                      //11
            Console.WriteLine(s4.GetLength());                      //9
            Console.WriteLine(s5.GetLength());                     // 4


            StringDS s6 = s3.SubString(7, 4);               
            Console.WriteLine(s6.ToString());                     //girl


            StringDS s7 = s4.SubString(3, 2);
            Console.WriteLine(s7.ToString());                     // g
           
            Console.WriteLine(StringDS.Concat(s4, s5).ToString());//the great wall


            Console.WriteLine(s4.Compare(new StringDS("the")));//1
            Console.WriteLine(s4.Compare(new StringDS("a")));//1
            Console.WriteLine(s4.Compare(new StringDS("z")));//-1
            Console.WriteLine(s4.Compare(new StringDS("the great")));//0
            Console.WriteLine(s5.Compare(new StringDS("     wall")));//0


            Console.WriteLine(s3.IndexOf(new StringDS("girl")));  //7
            Console.WriteLine(s4.IndexOf(new StringDS("the")));   //0
            Console.ReadKey();


        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值