自己实现的String结构类型的基本方法C#

参考:http://www.cnblogs.com/LiangShanCamp/p/4095018.html;

原文居然没注释(╯‵□′)╯︵┻━┻

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCLString
{
    class StringDataStructure
    {
        private char[] data;//用来存放字符串的char字符
        public StringDataStructure(char[] array)
        {
            data = new char[array.Length];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = array[i];

            }
        }
        public StringDataStructure(string str)
        {
            data = new char[str.Length];
            for(int i=0;i<data.Length;i++)
            {
                data[i] = str[i];
            }

        }
        public char this[int index]
        {
            get
            {
                return data[index];
            }
        }

        public int GetLength()
        {
            return data.Length;
        }
        /// <summary>
        ///
        /// 如果比较结果一样 return0
        /// 如果当前字符小于 s,返回-1
        /// 如果当前字符大鱼s,return 1
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public int Compare(StringDataStructure s)
        {
            int len = this.GetLength() < s.GetLength() ? this.GetLength() : s.GetLength(); //去得两值中最小的长度
            int index = -1;//存储不相等的字符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 StringDataStructure Substring(int index,int length)
        {
            char[] newData = new char[length];
            for(int i=index;i<index+length;i++)//i从索引位置开始遍历,读取length长度的子串
            {
                newData[i-index] = data[i];
            }
            return new StringDataStructure(newData);
        }
        public static  StringDataStructure Concat(StringDataStructure s1,StringDataStructure s2)
        {
            char[] newData = new char[s1.GetLength() + s2.GetLength()];//总长度
            for(int i=0;i<s1.GetLength();i++)//0-s1.lengthh-1
            {
                newData[i] = s1[i];
            }
            for(int i=s1.GetLength();i<newData.Length;i++)//s1.length-s1.length+s2.length
            {
                newData[i] = s2[i - s1.GetLength()];

            }
            return new StringDataStructure(newData);
        }
        public string ToString ()
        {
            return  new string (data);//通过string的构造方法将数组转化成字符串
        }
        /// <summary>
        /// 实现String类的替换方法
        /// </summary>
        /// <param name="s">原字串</param>
        /// <param name="r">替换后的字串</param>
        /// <param name="t">要被替换的字串</param>
        /// <returns></returns>
        public static string Replace(StringDataStructure s, StringDataStructure r, StringDataStructure t)
        {

            char[] datas = new char[s.GetLength()];
            char[] datar = new char[r.GetLength()];
            char[] datat = new char[t.GetLength()];
            for (int i = 0; i < datas.Length; i++)
            {
                datas[i] = s[i];

            }
            for (int i = 0; i < datar.Length; i++)
            {
                datar[i] = r[i];

            }
            for (int i = 0; i < datat.Length; i++)
            {
                datat[i] = t[i];

            }
            //获得要替换的原数据s,替换数据r,被替换数据t
           List<char> newCharList=new List<char>();//用一个char类型的List存放,(用char数组没有add方法方便(自动递增))
            for (int i = 0; i < datas.Length; i++)
            {
                if (datas[i] == datat[0])//遍历原数据,找到与被替换数据第一个
                {
                    bool IsReplace = false;  //标志当前的i位置是不是要替换的位置
                    for (int j = 0; j < datat.Length; j++)//遍历被替换的数据
                    {
                        if (((i + j) < datas.Length) && (datas[i + j] == datat[j]))
                        {
                            IsReplace = true;//只有当该位置不超过原数据的长度并且当前位置原元素与被替换数据所存的char类型字符相等时才设为true
                        }
                        else
                        {
                            IsReplace = false;//当前位置不相等,说明不是要替换的数据串,直接跳出循环
                            break;
                        }
                    }
                    if (IsReplace)//如果是要替换的char[i]
                    {
                      i += datat.Length - 1;//当前位置要加上被替换的数据的长度的位置,不然只是替换了你要被替换的数据的第一个char字符
                        for (int k = 0; k < datar.Length; k++)
                        {
                           newCharList.Add(datar[k]);//遍历你替换后的数据,添加到List里面
                        }
                    }
                    else
                    {
                        newCharList.Add(datas[i]);//不然添加原数据的char[i]位置的字符
                    }
                }
                else
                {
                    newCharList.Add(datas[i]);//不然添加原数据的char[i]位置的字符
                }
            }
            return new string(newCharList.ToArray());//返回new一个字符串里面存放这个List转为char[]数组的样子
        }
        public int IndexOf(StringDataStructure 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])//j-i表示比较时主串进行到的位置
                    {
                        isEqual = false;
                    }
                }
                if (isEqual)
                {
                    return i;
                }
                else
                { continue; }
            }
            return -1;//没有返回-1
        }
          
    }
       
}
总结:通过自己实现String结构,我发现那些表面上只要一个函数的事情,背后是逻辑严密复杂的算法和存储结构,不禁对那些写类库的人对钦佩

posted on 2017-05-09 22:05  微小的世界  阅读(764)  评论(0)    收藏  举报