html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

把一个字符插入到一个升序排列的字符串中_.NET教程_编程技术

[ ] 已经帮助:人解决问题
  1. namespace Tools.Module  
  2. {  
  3.     public class Tools  
  4.    {  
  5.          public Tools()  
  6.         {  
  7.          }  
  8.  
  9.         public static string GetStrFromStr(string src,int Index)  
  10.        {  
  11.              if(src.IndexOf(",")==0) src = src.Remove(0,1);  
  12.              for(int i=0;i<Index;i++)  
  13.             {  
  14.                  src = src.Remove(0,1+src.IndexOf(","));  
  15.             }  
  16.            if(src.IndexOf(",")<0) return src;  
  17.            else return src.Substring(0,src.IndexOf(","));  
  18.         }  
  19.        /// <summary>  
  20.        /// 计算出某字符在一个升序排序字符串中应处的位置.  
  21.        /// </summary>  
  22.        /// <param name="src">字符串</param>  
  23.        /// <param name="c">字符</param>  
  24.        /// <returns>该字符应处的位置</returns>  
  25.        public static int GetTheSortedIndex(string str,char c)  
  26.        {  
  27.              int iLength = str.Length;  
  28.              if(iLength == 0) return 0;  
  29.              if(iLength == 1 && str[0] >= c) return 0;  
  30.             else if(iLength == 1 && str[0] < c) return 1;  
  31.  
  32.             int index = iLength/2;  
  33.             if(str[index]>c)  
  34.             {  
  35.                   return GetTheSortedIndex(str.Substring(0,index),c);  
  36.              }  
  37.             else if(str[index]<c)  
  38.             {  
  39.                   return index+1+GetTheSortedIndex(str.Substring(index+1,iLength-index-1),c);  
  40.              }  
  41.              else 
  42.                  return index;  
  43.         }  
  44.         /// <summary>  
  45.         /// 把一个字符插入到一个升序排列的字符串中  
  46.         /// </summary>  
  47.         /// <param name="c">要插入的字符</param>  
  48.         /// <param name="str">目的字符串</param>  
  49.         public static void InsertCharIntoSortedString(char c,ref string str)  
  50.         {  
  51.              if(str==null  str.Length==0 )  
  52.             {  
  53.                  str=c.ToString();  
  54.                   return;  
  55.              }  
  56.  
  57.               for(int i=0;i<str.Length;i++)  
  58.               {  
  59.                     if(str[i]==c) return;  
  60.                 }  
  61.      
  62.               int index = GetTheSortedIndex(str,c);  
  63.              str = str.Insert(index,c.ToString());  
  64.         }  
  65.     }  
  66. }  
  67.  
vp5HTML5中文学习网 - HTML5先行者学习网
vp5HTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助