Exemplo n.º 1
0
        /// <summary>
        /// 字符串是否与数组中的任意一项相等
        /// </summary>
        /// <param name="s"></param>
        /// <param name="option">大小写选项</param>
        /// <param name="array"></param>
        /// <returns></returns>
        public static bool In(this string s, CaseOptions option, params string[] array)
        {
            StringComparison comparison = option.ToStringComparison();

            foreach (string i in array)
            {
                if (i.Equals(s, comparison))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 替换文本中的字符串
        /// </summary>
        /// <param name="s">文本</param>
        /// <param name="oldValue">要替换的字符串</param>
        /// <param name="newValue">取而代之的字符串</param>
        /// <param name="caseOption">大小写选项</param>
        /// <returns>替换之后的文本</returns>
        public static string Replace(this string s, string oldValue, string newValue, CaseOptions caseOption)
        {
            //根据大小写选项决定字符串查找选项
            StringComparison comparison = caseOption.ToStringComparison();

            //查找起始索引
            int startIndex = 0;

            //找到的旧值索引
            int oldValueIndex = 0;

            //替换处理之后的结果
            string result = "";

            //在文本中查找要替换的字符串
            while ((oldValueIndex = s.IndexOf(oldValue, startIndex, comparison)) >= 0)
            {
                //把旧值索引前的内容追加到结果中
                (oldValueIndex - startIndex).Each(index =>
                {
                    result += s[startIndex + index];
                });

                //再追加新值
                result += newValue;

                //更新起始索引
                startIndex = oldValueIndex + oldValue.Length;
            }

            //把剩余的内容追加到结果中
            (s.Length - startIndex).Each(index =>
            {
                result += s[startIndex + index];
            });

            return(result.ToString());
        }
Exemplo n.º 3
0
        /// <summary>
        /// 分隔字符串
        /// </summary>
        /// <param name="s">要分隔的字符串</param>
        /// <param name="separator">分隔符</param>
        /// <param name="caseOption">大小写选项</param>
        /// <param name="separatorOption">分隔符选项</param>
        /// <returns>分隔后的字符串数组</returns>
        public static string[] Split(this string s, string separator, CaseOptions caseOption, SeparatorOptions separatorOption)
        {
            //根据大小写选项决定字符串查找选项
            StringComparison comparison = caseOption.ToStringComparison();

            //查找起始索引
            int startIndex = 0;

            //找到的分隔符索引
            int separatorIndex = 0;

            //分隔出来的字符串列表
            List <string> result = new List <string>();

            //在文本中查找分隔符
            while ((separatorIndex = s.IndexOf(separator, startIndex, comparison)) > -1)
            {
                //字符串截取起始索引
                int substringStartIndex = startIndex;

                //字符串截取长度
                int substringCount = separatorIndex - startIndex;

                //根据分隔符选项决定截取起始索引和截取长度
                switch (separatorOption)
                {
                case SeparatorOptions.NoSeparator:
                    break;

                case SeparatorOptions.PostSeparator:
                    substringCount += separator.Length;
                    break;

                case SeparatorOptions.PreSeparator:
                    substringStartIndex -= separator.Length;
                    substringCount      += separator.Length;
                    break;
                }

                //如果分隔符不在位置0
                //把分隔符前的内容加入结果队列
                if (separatorIndex > 0)
                {
                    result.Add(s.Substring(substringStartIndex, substringCount));
                }

                //更新起始索引
                startIndex = separatorIndex + separator.Length;
            }

            //剩余长度
            int restCount = s.Length - startIndex;

            //如果还有剩余内容
            //把剩余的内容加入结果队列
            if (restCount > 0)
            {
                result.Add(s.Substring(startIndex, restCount));
            }

            return(result.ToArray());
        }