最近面试,总是失败,就的天天啃书了数据结构,设计模式,C#高级,javascript高级样样都的看,现在的企业简直就是要全能的嘛!
哎.....
上代码,欢迎拍砖,性能需优化...

解决方案 »

  1.   


     public override string ToString()
            {
                StringBuilder str = new StringBuilder();
                foreach (var item in this)
                {
                    str.Append(item);
                }
                return str.ToString();
            }        #region IEnumerable<string> 成员        public IEnumerator<char> GetEnumerator()
            {
                for (int i = 0; i < this.GetLength(); i++)
                {
                    yield return this[i];
                }
            }        #endregion        #region IEnumerable 成员        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }        #endregion
      

  2.   


     public class StringDS : IEnumerable<char>
        {        private char[] data;       /// <summary>
           /// 创建索引
           /// </summary>
           /// <param name="index"></param>
           /// <returns></returns>
            public char this[int index]
            {
                get
                {
                    return data[index];
                }
                set
                {
                    data[index] = value;
                }
            }        /// <summary>
            /// char数组构造
            /// </summary>
            /// <param name="arr"></param>
            public StringDS(char[] arr)
            {
                data = new char[arr.Length];
                for (int i = 0; i < arr.Length; i++)
                {
                    data[i] = arr[i];
                }        }        /// <summary>
            /// StringDS构造
            /// </summary>
            /// <param name="str"></param>
           public StringDS(StringDS str)
           {
               data = new char[str.GetLength()];
               for (int i = 0; i < str.data.Length; i++)
               {
                   data[i] = str.data[i];
               }
            }        /// <summary>
            /// 长度构造
            /// </summary>
            /// <param name="len"></param>
            public StringDS(int len)
            {
                data = new char[len];
            }
            /// <summary>
            /// 字符串长度
            /// </summary>
            /// <returns></returns>
            public int GetLength()
            {
                return data.Length;
            }
            /// <summary>
            /// 两个字符串是否相等
            /// </summary>
            /// <param name="s">要比较的串</param>
            /// <returns></returns>
            public bool Compare(StringDS s)
            {
                bool flag=true ;
                if (this.GetLength() != s.GetLength())
                {
                    flag= false;
                }
                else
                {
                    for (int i = 0; i < this.GetLength(); i++)
                    {
                        if (this[i] != s[i])
                        {
                            flag = false;
                            break;
                        }
                    }
                }
                return flag;        }
            /// <summary>
            /// 截取字符串
            /// </summary>
            /// <param name="len">截取的长度</param>
            /// <param name="startIndex">开始截取位置</param>
            /// <returns></returns>
            public StringDS SubString(int len, int startIndex = 1)
            {
                if (startIndex < 0 || startIndex > this.GetLength() - 1||len<0||len>this.GetLength()-startIndex)
                {
                    Console.WriteLine("Error");
                    return null;
                }
                StringDS s = new StringDS(len);
                for (int i = 0; i < len; i++)
                {
                    s[i] = this[i + startIndex - 1];
                }
                return s;
            }
            /// <summary>
            /// 字符串替换
            /// </summary>
            /// <param name="s">原字符串</param>
            /// <param name="rs">替换字符串</param>
            /// <returns></returns>
            public StringDS Replace(StringDS s, StringDS rs)
            {
                if (this.GetLength() < s.GetLength())
                {
                    Console.WriteLine("Error");
                    return null;
                }
                //先找s在原串中出现的次数
                int index=0;
                int slen = s.GetLength(); //被替换串的长度
                int rlen = rs.GetLength();//替换串的长度
                int length = this.GetLength(); //原串长度
                bool flag=false   ;
                for (int i = 0; i < length; i++)
                {
                    flag = true;
                    for (int j = 0; j < slen; j++)
                    {
                        if (this[i + j] != s[j])
                        {
                            flag = false;
                            break;
                        }
                     }
                    if (flag)
                    {
                        i = i + slen;
                        index = index + 1;
                    }
                }
                //得到原串中s出现的次数就能得到替换后字符串的长度=原串长+(替换串长-被替换串长)*出现次数
                StringDS ds = new StringDS(this.GetLength() + ((rs.GetLength() - slen) * index));
                int dslen=ds.GetLength();//替换后串长
                 index = 0; //index置0,控制新串小标
                for (int i = 0; i < length-1; i++)
                {
                    
                    flag = true;
                    for (int j = 0; j < slen; j++)  //和被替换串比较
                    {
                        if (this[i + j] != s[j])
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) //和被替换串匹配
                    {
                        for (int m = 0; m < rlen; m++) //将替换串赋给新串
                        {
                            ds[index + m] = rs[m];
                        }
                        i = i + slen-1 ;//将原串的下标加上被替换串的长度减1 
                        index = index + rlen-1; //将新串下标加替换串长度减1
                    }
                    else //不匹配则将原串值直接给新串
                    {
                            ds[index] = this[i]; 
                        
                    }
                    index = index + 1;
                }
                    
                return ds;
                
            }
            /// <summary>
            /// 追加串
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public StringDS Append(StringDS s)
            {
               
                int length = this.GetLength(); //原串长
                int slength = s.GetLength();//追加串长
                if (length == 0)
                {
                    return s;
                }
                if (slength == 0)
                {
                    return this;
                }
                int newLength = length + slength;
                StringDS newStr = new StringDS(newLength); //构造新串 
                for (int i = 0; i < length; i++) //将原串赋给新串
                {
                    newStr[i] = this[i];
                }
                int index = length - 1;
                for (int i = 0; i < slength; i++) //将追加串赋给新串
                {
                    newStr[++index] = s[i];
                }
                return newStr;
            }
         
     }
      

  3.   


       /// <summary>
            /// 插入新串
            /// </summary>
            /// <param name="s">插入的串</param>
            /// <param name="index">插入的位置</param>
            /// <returns></returns>
            public StringDS Insert(StringDS s,int index)
            {
                int length = this.GetLength(); //原串长
                int slength = s.GetLength(); //插入串长
                if (length == 0||index>length||index<0)
                {
                    Console.WriteLine("Error");
                    return null;
                }
                StringDS newStr = new StringDS(length+slength); //构造新串
                //1.将原串的0到插入的位置赋给新串
                //2.将插入串赋给新串
                //3.将原串插入位置后的赋给新串
                for (int i = 0; i < index; i++)
                {
                    newStr[i] = this[i];
                }
                for (int i = index; i < slength; i++)
                {
                    newStr[i] = s[i];
                }
               
                for (int i = index; i < length; i++)
                {
                    newStr[i + slength] = this[i];
                }
                return newStr;
            }
            /// <summary>
            /// 移除
            /// </summary>
            /// <param name="startIndex">开始位置</param>
            /// <param name="endIndex">结束位置</param>
            /// <returns></returns>
            public StringDS Remove(int startIndex,int endIndex)
            {
                int length = this.GetLength(); //原串长
                if (length == 0 || startIndex <= 0 || endIndex <= 0 || (startIndex >= endIndex))
                {
                    Console.WriteLine("Error");
                    return null;
                }
                int newLength = length - endIndex + startIndex;
                StringDS newStr = new StringDS(newLength); //构造新串
                //1.将原串0到删除的startIndex开始位置赋给新串
                //2.将原串删除结束位置endIndex之后赋给新串
                for (int i = 0; i < startIndex; i++)
                {
                    newStr[i] = this[i];
                }
                for (int i = endIndex; i < length; i++)
                {
                    newStr[i] = this[i];
                }
                return newStr;
            }
            /// <summary>
            /// 分割串
            /// </summary>
            /// <param name="s">以什么分割</param>
            /// <returns></returns>
            public StringDS[] Split(StringDS s)
            {
                StringDS ds = this.Append(new StringDS(s)); //在末尾追加一个分割串,后面处理方便
                int length = ds.GetLength();
                int slength = s.GetLength();
                bool flag = false;
                int index = 0;
                for (int i = 0; i < length ; i++)
                {                flag = true;
                    for (int j = 0; j < slength; j++)  //和分割串比较
                    {
                        if (ds[i + j] != s[j])
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                    {
                        index = index + 1;
                    }              
                }
                StringDS[] arr = new StringDS[index]; //初始化分割后数组
                StringDS tempstr; 
               
                index=0;//分割数组计数器
                int tempindex = 0;//计算每次分割在原字符串中的位置
                int tempi=0; //没一次分割串处理计数器
                
                for (int i = 0; i < length; i++)
                {
                    flag = true;
                    for (int j = 0; j < slength; j++)  //和分割串比较
                    {
                        if (ds[i + j] != s[j])
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) //和分割串匹配
                    {
                        tempi = 0;
                        tempstr = new StringDS(i - tempindex); //构造临时StringDS存放串 在放入数组
                         for (int m = tempindex; m < i; m++)
                        {
                            tempstr[tempi] = this[m];
                            ++tempi;
                        }
                         tempindex = i + slength;
                        arr[index] = tempstr;
                        ++index;
                    }
                   
                }
                return arr;
            }
      

  4.   

     支持学习提高。但注释好多废话哦。。真的。。你用 StringBuilder 来做性能会提高些。反正都自制了。。
      

  5.   

    StringBuilder的Append不知道是怎么实现的..它不需要用新串...
      

  6.   

    http://www.cnblogs.com/Conception/archive/2012/02/14/2351769.html
      

  7.   

    哈哈,你可以反编译看看.net自带的string是如何实现的
      

  8.   

    啊撒旦和
    1.这里发言,表示您接受了CSDN社区的用户行为准则。
    2.请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。
    3.转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。
      

  9.   

    xuexi1.这里发言,表示您接受了CSDN社区的用户行为准则。
    2.请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。
    3.转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。
      

  10.   

    楼主有空做下性能测试哈哈。与net自带的比较一下。
      

  11.   

    楼主有空做下性能测试哈哈。与net自带的比较一下。
      

  12.   


    这些字符串操作,.net自己有吗?   如果没有,我就拷贝下来,以后就可以省事了。
      

  13.   


    .net 肯定有啊 真是的
      

  14.   

    咳,又重复发明轮子了,replace, splite。.net没提供????
      

  15.   

    这里发言,表示您接受了CSDN社区的用户行为准则。
    请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。
    转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者
      

  16.   

    呵呵,lz幸好没有到我们公司,我们公司说现在业务已java为主,.NET项目组全部改写java!
      

  17.   


    用过.Net的string吗?用过的话 你就知道这些最基本的方法,虽然写的效率不高,但是有这么一种方法...
      

  18.   

    不错啊 学习了 话说昨天还弄算法来着 但是c# 自带的string 操作少的可怜啊
      

  19.   

    我不反对楼主自己封装,但是我觉得楼主对string的看法有问题。在java最开始的时候操作“字符串”是相当痛苦的,c#只是提供一种简单易用的字符串操作类,一直到现在都是一种传承。
    我觉得不是微软没实力去扩充这些东西,只是觉得没必要。而您提供的好多方法其实可以用扩展方法的形式直接挂靠在string上就可以了,至于其它的,也可以挂靠在stringbuilder上。就像我们现在看到的好多三方控件一样,微软之所以不做了,是不想和下游厂商争小利。其实人就是这样:人家都做了,你说人家垄断,人家不做吧,又成了别人说三道四的理由了