यहां मेरी विस्तार विधि है, जो sntbob's की पुनरावृत्ति के साथ Tom Beech's को जोड़ती है, और ksun ने बग को क्लीनर फ़िक्स किया है।
कोड:
public static string Replace(this string source, string oldString,
string newString, StringComparison comparison)
{
int index = source.IndexOf(oldString, comparison);
while (index > -1)
{
source = source.Remove(index, oldString.Length);
source = source.Insert(index, newString);
index = source.IndexOf(oldString, index + newString.Length, comparison);
}
return source;
}
उपयोग:
string source = "banana";
Console.WriteLine(source.Replace("AN", "banana", StringComparison.OrdinalIgnoreCase));
परिणाम:
bbananabananaa
और, यदि आप अभी भी पुनरावर्ती प्रकृति चाहते हैं, तो सेशन होने के लिए राष्ट्रीय:
कोड:
public static string Replace(this string source, string oldString,
string newString, StringComparison comparison,
bool recursive = true)
{
int index = source.IndexOf(oldString, comparison);
while (index > -1)
{
source = source.Remove(index, oldString.Length);
source = source.Insert(index, newString);
if (!recursive)
{
return source;
}
index = source.IndexOf(oldString, index + newString.Length, comparison);
}
return source;
}
उपयोग:
string source = "banana";
Console.WriteLine(source.Replace("AN", "banana", StringComparison.OrdinalIgnoreCase, false));
परिणाम:
bbananaana
स्रोत
2015-09-22 20:13:00
संभावित डुप्लिकेट [क्या स्ट्रिंग का कोई विकल्प है। बदलें जो केस असंवेदनशील है?] (Https://stackoverflow.com/questions/244531/is-there-an-alternative-to-string-replace-that -इस-केस-असंवेदनशील) –