others - 在不使用 正规表达式的情况下.NET中是否存在不区分大小写的字符串替换?

68 0

我最近不得不在.net中执行一些字符串替换,并发现自己为此目的开发了一个正则表达式替换函数。 让它工作后,我忍不住认为.Net应该有一个内置不区分大小写的替代操作吧? 我错过了吗?

当然, 有许多其他字符串操作, 支持不区分大小写的比较,例如;


var compareStrings = String.Compare("a","b", blIgnoreCase);


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



那么必须有一个内置的等效替换?

时间: 原作者:

132 2

http://www.codeproject.com/Messages/1835929/this-one-is-even-faster-and-more-flexible-modified.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();


}



应该是最快的不过我还没检查过。

否则你应该用Simon的建议,并使用 VisualBasic替换函数 。 这是我经常做的,因为它不区分大小写的能力。


string s ="SoftWare";


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



你必须添加对 Microsoft.VisualBasic dll的引用。

原作者:
80 5

这并不理想,但是你可以导入 Microsoft.VisualBasic 并使用 Strings.Replace 执行这里操作。 否则我认为这是自己写或坚持使用正则表达式的情况。

原作者:
83 1

这是一个扩展方法不确定我在哪里找到它。


public static class StringExtensions


{


 public static string Replace(this string originalString, string oldValue, string newValue, StringComparison comparisonType)


 {


 int startIndex = 0;


 while (true)


 {


 startIndex = originalString.IndexOf(oldValue, startIndex, comparisonType);


 if (startIndex == -1)


 break;



 originalString = originalString.Substring(0, startIndex) + newValue + originalString.Substring(startIndex + oldValue.Length);



 startIndex += newValue.Length;


 }



 return originalString;


 }



}



原作者:
55 1


Public Function Replace(ByVal originalString As String, 


 ByVal oldValue As String, 


 ByVal newValue As String, 


 ByVal comparisonType As StringComparison) As String


 If Not String.IsNullOrEmpty(originalString) AndAlso 


 Not String.IsNullOrEmpty(oldValue) AndAlso 


 newValue IsNot Nothing Then


 Dim startIndex As Int32



 Do While True


 startIndex = originalString.IndexOf(oldValue, startIndex, comparisonType)


 If startIndex = -1 Then Exit Do


 originalString = originalString.Substring(0, startIndex) & newValue & 


 originalString.Substring(startIndex + oldValue.Length)


 startIndex += newValue.Length


 Loop


 End If



 Return originalString


End Function



原作者:
147 2


public static string Replace(this string originalString, string oldValue, string newValue, StringComparison comparisonType)


{


 if (originalString == null)


 return null;


 if (oldValue == null)


 throw new ArgumentNullException("oldValue");


 if (oldValue == string.Empty)


 return originalString;


 if (newValue == null)


 throw new ArgumentNullException("newValue");



 const int indexNotFound = -1;


 int startIndex = 0, index = 0;


 while ((index = originalString.IndexOf(oldValue, startIndex, comparisonType))!= indexNotFound)


 {


 originalString = originalString.Substring(0, index) + newValue + originalString.Substring(index + oldValue.Length);


 startIndex = index + newValue.Length;


 }



 return originalString;


}



Replace("FOOBAR","O","za", StringComparison.OrdinalIgnoreCase);


//"FzazaBAR"



Replace("","O","za", StringComparison.OrdinalIgnoreCase);


//""



Replace("FOO","BAR","", StringComparison.OrdinalIgnoreCase);


//"FOO"



Replace("FOO","F","", StringComparison.OrdinalIgnoreCase);


//"OO"



Replace("FOO","","BAR", StringComparison.OrdinalIgnoreCase);


//"FOO"



96 5

我知道框架中没有固定的实例,但这里是另一个扩展方法版本,只有少量语句( 虽然也许不是最快的) 为了好玩 。 更多版本的替换函数发布在 http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx是否有替代 string.Replace 不区分大小写 ? 也。


public static string ReplaceIgnoreCase(this string alterableString, string oldValue, string newValue){


 if(alterableString == null) return null;


 for(


 int i = alterableString.IndexOf(oldValue, System.StringComparison.CurrentCultureIgnoreCase);


 i> -1;


 i = alterableString.IndexOf(oldValue, i+newValue.Length, System.StringComparison.CurrentCultureIgnoreCase)


 ) alterableString =


 alterableString.Substring(0, i)


 +newValue


 +alterableString.Substring(i+oldValue.Length)


 ;


 return alterableString;


}



原作者:
61 0

返回一个字符串,其中指定的子字符串已被另一个子字符串替换为指定次数
它有一个可选的Microsoft.VisualBasic.CompareMethod paramater,用于指定评估子字符串时使用的比较类型


 Dim mystring As String ="One Two Three"


 mystring = Replace(mystring,"two","TWO", 1,, CompareMethod.Text)



...