C#替換字符串起始/結尾指定的字符串


     #region 替換字符串起始位置(開頭)中指定的字符串
        /// <summary>  
        /// 替換字符串起始位置(開頭)中指定的字符串  
        /// </summary>  
        /// <param name="s">源串</param>  
        /// <param name="searchStr">查找的串</param>  
        /// <param name="replaceStr">替換的目標串</param>  
        /// <returns></returns>  
        public static string CutStarStr(string s, string searchStr, string replaceStr)
        {
            var result = s;
            try
            {
                if (string.IsNullOrEmpty(result))
                {
                    return result;
                }
                if (s.Length < searchStr.Length)
                {
                    return result;
                }
                if (s.IndexOf(searchStr, 0, searchStr.Length, StringComparison.Ordinal) > -1)
                {
                    result = s.Substring(searchStr.Length);
                }
                return result;
            }
            catch (Exception e)
            {
                return result;
            }
        }
        #endregion

        #region 替換字符串末尾位置中指定的字符串
        /// <summary>  
        /// 替換字符串末尾位置中指定的字符串  
        /// </summary>  
        /// <param name="s">源串</param>  
        /// <param name="searchStr">查找的串</param>  
        /// <param name="replaceStr">替換的目標串</param>  
        public static string CutEndStr(string s, string searchStr, string replaceStr)
        {
            var result = s;
            try
            {
                if (string.IsNullOrEmpty(result))
                {
                    return result;
                }
                if (s.Length < searchStr.Length)
                {
                    return result;
                }
                if (s.IndexOf(searchStr, s.Length - searchStr.Length, searchStr.Length, StringComparison.Ordinal) > -1)
                {
                    result = s.Substring(0, s.Length - searchStr.Length);
                }
                return result;
            }
            catch (Exception e)
            {
                return result;
            }
        }
        #endregion
  

上海.NET招聘:上海.NET招聘

鄭州.NET招聘:鄭州.NET招聘

深圳.NET招聘:深圳.NET招聘


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM