是否有一个区分大小写的字符串在.net替换,而无需使用正则表达式? [英] Is there a case insensitive string replace in .Net without using Regex?

查看:113
本文介绍了是否有一个区分大小写的字符串在.net替换,而无需使用正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近不得不执行.NET一些字符串替换和发现自己制定一个定期EX pression替换功能用于此目的。得到它的工作我都忍不住想后必须有一个内置在.net中不区分大小写置换手术,我很想念?

I recently had to perform some string replacements in .net and found myself developing a regular expression replacement function for this purpose. After getting it to work I couldn't help but think there must be a built in case insensitive replacement operation in .Net that I'm missing?

当然,当有支持不区分大小写作仪等其他许多字符串操作;

Surely when there are so many other string operations that support case insensitive comparission such as;

var compareStrings  = String.Compare("a", "b", blIgnoreCase);
var equalStrings    = String.Equals("a", "b", StringComparison.CurrentCultureIgnoreCase);

那么必须有一个内置相当于替代?

then there must be a built in equivalent for replace?

推荐答案

找到了一个在这里评论:<一href="http://www.$c$cproject.com/KB/string/fastestcscaseinsstringrep.aspx">http://www.$c$cproject.com/KB/string/fastestcscaseinsstringrep.aspx

Found one in the comments here: http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx

static public string Replace(string original, string pattern, string replacement, StringComparison comparisonType)
{
     return Replace(original, pattern, replacement, comparisonType, -1);
}

static public string Replace(string original, string pattern, string replacement, StringComparison comparisonType, int stringBuilderInitialSize)
{
     if (original == null)
     {
         return null;
     }

     if (String.IsNullOrEmpty(pattern))
     {
         return original;
     }


     int posCurrent = 0;
     int lenPattern = pattern.Length;
     int idxNext = original.IndexOf(pattern, comparisonType);
     StringBuilder result = new StringBuilder(stringBuilderInitialSize < 0 ? Math.Min(4096, original.Length) : stringBuilderInitialSize);

     while (idxNext >= 0)
     {
        result.Append(original, posCurrent, idxNext - posCurrent);
        result.Append(replacement);

        posCurrent = idxNext + lenPattern;

        idxNext = original.IndexOf(pattern, posCurrent, comparisonType);
      }

      result.Append(original, posCurrent, original.Length - posCurrent);

      return result.ToString();
}

应该是最快的,但我没有检查。

Should be the fastest, but i haven't checked.

否则,你应该做的西蒙建议,并使用的VisualBasic替换功能。这是我一直这样做,因为它不区分大小写的能力(我通常一个VB.Net程序员)。

Otherwise you should do what Simon suggested and use the VisualBasic Replace function. This is what i always do because of its case-insensitive capabilities(i'm usually a VB.Net programmer).

string s = "SoftWare";
s = Microsoft.VisualBasic.Strings.Replace(s, "software", "hardware", 1, -1, Constants.vbTextCompare);

您必须添加一个引用到Microsoft.VisualBasic程序的DLL。

You have to add a reference to the Microsoft.VisualBasic dll.

这篇关于是否有一个区分大小写的字符串在.net替换,而无需使用正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆