StringHelper

        /// <summary>
        /// 文本取左边
        /// </summary>
        /// <param name="str"></param>
        /// <param name="Left"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public static string GetLeft(string str, string Left, int pos = 0)
        {
            if (pos < 0)
            {
                pos = 0;
            }
            int index = str.IndexOf(Left, pos, StringComparison.Ordinal);
            if (index == -1)
            {
                return "";
            }
            return str.Substring(0, index);
        }

        /// <summary>
        /// 取文本右边
        /// </summary>
        /// <param name="str"></param>
        /// <param name="right"></param>
        /// <param name="pos">起始寻找位置</param>
        /// <returns></returns>
        public static string GetRight(string str, string right, int pos = 0)
        {
            pos = pos == 0 ? right.Length : pos;
            int index = str.IndexOf(right, pos, StringComparison.Ordinal);
            if (index == -1)
            {
                return "";
            }
            return str.Substring(index + right.Length);
        }
        /// <summary>
        /// 取文本中间
        /// </summary>
        /// <param name="allStr">原字符</param>
        /// <param name="firstStr">前面的文本</param>
        /// <param name="lastStr">后面的文本</param>
        /// <returns></returns>
        public static string GetMid(string allStr, string firstStr, string lastStr)
        {
            //取出前面的位置
            int index1 = allStr.IndexOf(firstStr);
            if (index1 != -1)
            {
                index1 = index1 + firstStr.Length;
            }
            //取出后面的位置
            int index2 = allStr.IndexOf(lastStr, index1);

            if (index1 == -1 || index2 == -1)
            {
                return "";
            }
            //取出文本
            return allStr.Substring(index1, index2 - index1);
        }

 

你可能感兴趣的:(StringHelper)