zoukankan      html  css  js  c++  java
  • C# 如何高效替换一个字符串中的全部某字符?

    String.Replace也是全部替换。
    如果您需要替换复杂的内容,可以使用正则表达式。
    正则对复杂字串的替换效率高。C#中的正则表达式默认贪婪算法,总试图匹配更多的字符,所以若是简单的替换一个或几个字符,还是不要用比较好。
    Regex.Replace()
    需要引用System.Text.RegularExpression命名空间。

    C# String 正则 替换空格 tab字符 换行符 新行
    使用正则表达式匹配符"s",它将匹配任何空格包含在这个字符串里C#空格, tab字符, 换行符和新行(newline).
    string trim = Regex.Replace( text, @"s", "");

    例:c# 字符串多个TAB 替换成1个TAB

    string trim = Regex.Replace("system		threading_		thread_	currentthread		_currentculture_textinfo", @"		", "	");
    

    输出结果:

    system threading_ thread_ currentthread _currentculture_textinfo

  • 相关阅读:
    51.try块和catch块中return语句的执行
    17. 处理日期
    16.查找特定字符出现的次数
    15.字符串长度
    14.字符串拆分
    13.字符串比较
    12.幸运抽奖
    11.使用枚举
    10.获取系统时间
    MSSQL 判断临时表是否存在
  • 原文地址:https://www.cnblogs.com/wzihan/p/14869567.html
Copyright © 2011-2022 走看看