c# string 扩展

 c# string 扩展类 Extension

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

/// <summary>
/// string扩展
/// </summary>
public static class stringExtension
{

	
	
    /// <summary>
    /// 只包含 URL链接(网址)
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static bool IsURL(this string url)
    {
 
        string pattern = @"^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}\/?";
        Regex regex = new Regex(pattern);
        return regex.IsMatch(url);
    }
 
 
    /// <summary>
    /// 只包含中文
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static bool IsChines(this string url)
    {
 
        string pattern = @"^[\u4E00-\u9FA5]+$";
        Regex regex = new Regex(pattern);
        return regex.IsMatch(url);
    }
    
    /// <summary>
    /// 删除指定字符
    /// </summary>
    /// <param name="str"></param>
    /// <param name="del_str"></param>
    /// <returns></returns>
    public static string Del(this string str,string del_str)
    {
        str = str.Replace(del_str, "");
        return str;
    }
 
/// <summary>
/// 分割
/// </summary>
/// <param name="str"></param>
/// <param name="replacement"></param>
/// <returns></returns>
    public static string[] Split(this string str,string separator)
    {
       // System.StringSplitOptions.None 返回值包括含有空字符串的数组元素。
        //System.StringSplitOptions.RemoveEmptyEntries 返回值不包括含有空字符串的数组元素。
        string[] split_result = str.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
        return split_result;
    }
 
    /*
string fileName = "1?2.txt"
string path = @"D:\Download\" + DelValidFileName(fileName);
path = "D:\Download\12.txt"
    */
    /// <summary>
    /// 去除不可作为文件名的特殊符号
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static string DelValidFileName(this string fileName)
    {
        //Windows中文件名中的非法字符:/\:*?"<>|\r\n
        string pattern = @"[\/\:*?\""<>|\r\n]";
        return Regex.Replace(fileName, pattern,"");
    }
 

    /*
var a = "\r\n".IsEmptyLine();  //true
var a1 = "\n".IsEmptyLine();    //true
var a2 = "\r".IsEmptyLine();    //true
var b=  "\n8".IsEmptyLine();    //false
var c = "3\r\n".IsEmptyLine();    //false
     */
    /// <summary>
    /// 判断字符串是否为空行,"\r", "\n", "\r\n",""
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool IsEmptyLine(this string str)
    {
        string[] emptyLine = { "\r", "\n", "\r\n","" };
        for(int i=0;i<emptyLine.Length;i++ )
        {
            if (emptyLine[i] == str)
                return true;
        }
        return false;

        //方法2
        //var CR = "\r";
        //var LF = "\n";
        //var CRLF = "\r\n";
        //var CR_lines = line.Split(CR).ToList();
        //var LF_lines = line.Split(LF).ToList();
        //var CRLF_lines = line.Split(CRLF).ToList();
        //if (CR_lines.Count == 0 || LF_lines.Count == 0 || CRLF_lines.Count == 0)
        //    return true;
        //return false;
    }


/*
var c1 = " \r\n".IsEmptyLine_Contontaining_Blank_Character();//true
var c2 = " \t \t\r\n".IsEmptyLine_Contontaining_Blank_Character();//true
var c3 = "\t\r\n".IsEmptyLine_Contontaining_Blank_Character();//true
var c11 = " \r\n\v".IsEmptyLine_Contontaining_Blank_Character();//true
var c21 = " \t \t\r\n\f".IsEmptyLine_Contontaining_Blank_Character(); //true
var c211 = " \t \t\r.\n\f".IsEmptyLine_Contontaining_Blank_Character(); //false
var c31 = "\t\r\n1".IsEmptyLine_Contontaining_Blank_Character(); //false
*/
    /// <summary>
    /// 判断str是否为空白字符组成, 空白字符包括:回车符(\r)、换行符(\n)、水平制表符(\t)、垂直制表符(\v)、换页符(\f)、空格(" ")
    /// </summary>
    /// <param name="line"></param>
    /// <returns></returns>
    public static bool IsEmptyLine_Contontaining_Blank_Character(this string str)
    {
        //删除空白字符后,判断是否等于""
        var result = str;
        string[] emptyLine = { "\r\n", "\r", "\n", "\t" ," ", "\v", "\f", };
        for (int i = 0; i < emptyLine.Length; i++)
        {
            result = Regex.Replace(result, emptyLine[i], "");
        }

        var t2 = result =="";
        return t2;
    }


    /// <summary>
    /// 删除空行和空白字符行
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static string DelEmptyLines(this string content)
    {
        //必须分行处理
        //1.先判断换行的符号 是 1. \r\n 还是2. \n 还是3. \r
        var CR = "\r";
        var LF = "\n";
        var CRLF = "\r\n";
        var curr_CRLF = "";

        var CR_lines = content.Split(CR).ToList();
        var LF_lines = content.Split(LF).ToList();
        var CRLF_lines = content.Split(CRLF).ToList();



        List<string> maxLinesList =new List<string>();
        if(CR_lines.Count > LF_lines.Count && CR_lines.Count > CRLF_lines.Count)
        {
            maxLinesList = CR_lines;
            curr_CRLF = CR;

        }
        if (LF_lines.Count > CR_lines.Count && LF_lines.Count > CRLF_lines.Count)
        {
            maxLinesList = LF_lines;
            curr_CRLF = LF;
        }
        if (CRLF_lines.Count > LF_lines.Count && CRLF_lines.Count > CR_lines.Count)
        {
            maxLinesList = CRLF_lines;
            curr_CRLF = CRLF;
        }

        //两个一样多时
        if (CR_lines.Count == LF_lines.Count && CR_lines.Count > CRLF_lines.Count)
        {
            maxLinesList = LF_lines;
            curr_CRLF = LF;

        }
        if (LF_lines.Count == CR_lines.Count && LF_lines.Count > CRLF_lines.Count)
        {
            maxLinesList = LF_lines;
            curr_CRLF = LF;
        }
        if (CRLF_lines.Count == LF_lines.Count && CRLF_lines.Count > CR_lines.Count)
        {
            maxLinesList = CRLF_lines;
            curr_CRLF = CRLF;
        }

        //List遍历中删除指定元素
        for (int i = maxLinesList.Count - 1; i >= 0; i--)
        {
            var item = maxLinesList[i];
            var temp = item.IsEmptyLine_Contontaining_Blank_Character();
            if (temp)
                maxLinesList.RemoveAt(i);
        }

        var result = "";
        maxLinesList.ForEach(a => 
        {
            result += a + curr_CRLF;
        });
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值