按照标点的自动分段C#代码

按照标点进行段落排版的C#代码

 public static class FormatTxT
    {     
        public static bool after_is_en(this string str)
        {
            return Regex.IsMatch(str, ".*[a-zA-Z]$");
        }
        public static bool before_is_en(this string str)
        {
            return Regex.IsMatch(str, "^[a-zA-Z]");
        }
        public static bool str_is_split(this string str)
        {
            return Regex.IsMatch(str, ".*[。?!.?!:]$");
        }
        public static string format_txt(this string strlines)
        {
            string[] strings = strlines.Split(new string[] {System.Environment.NewLine}, StringSplitOptions.None);
            string childstr = "";
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < strings.Length; i++)
            {
                string currentstr = strings[i].Trim();
                if (i>0)
                {
                    string laststr = strings[i - 1].Trim();
                    if (currentstr.after_is_en() && laststr.before_is_en())
                    {
                        str.Append(" ");
                    }
                }
                str.Append(currentstr);
                if (currentstr.str_is_split())
                {
                    str.Append(System.Environment.NewLine);
                }
            }
            return str.ToString();
        }
    }
Last modification:May 27th, 2019 at 08:10 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment