String.Replace मामले की अनदेखी


214

मेरे पास एक स्ट्रिंग है जिसे "हैलो वर्ल्ड" कहा जाता है

मुझे "विश्व" शब्द को "csharp" में बदलने की आवश्यकता है

इसके लिए मैं उपयोग करता हूं:

string.Replace("World", "csharp");

लेकिन परिणामस्वरूप, मुझे स्ट्रिंग को प्रतिस्थापित करने की आवश्यकता नहीं है। कारण केस सेंसिटिविटी है। मूल स्ट्रिंग में "दुनिया" शामिल है, जबकि मैं "विश्व" को बदलने की कोशिश कर रहा हूं।

क्या स्ट्रिंग में इस मामले की संवेदनशीलता से बचने का कोई तरीका है। विधि का उपयोग करें?



जवाबों:


309

आप एक Regex का उपयोग कर सकते हैं और एक मामले को असंवेदनशील प्रतिस्थापित कर सकते हैं:

class Program
{
    static void Main()
    {
        string input = "hello WoRlD";
        string result = 
           Regex.Replace(input, "world", "csharp", RegexOptions.IgnoreCase);
        Console.WriteLine(result); // prints "hello csharp"
    }
}

19
रेगेक्स भाषा तत्वों के साथ काम नहीं करता है , इसलिए यह सार्वभौमिक तरीका नहीं है। स्टीव बी का जवाब सही है।
AsValeO

1
तो आप बेहतर लिखेंगे hello. world?या कुछ और नहीं करेंगे जिसमें रेगेक्स ऑपरेटर हों।
सेबस्टियन मच

बस अगर किसी को आगे पढ़ने की इच्छा नहीं थी, तो यह 2011 में स्वीकृत जवाब था और इसमें भारी संख्या में वोट थे। यह ठीक काम करता है यदि आपको केवल अल्फ़ान्यूमेरिक को बदलना है। हालाँकि, यदि आपको किसी विराम चिह्न को बदलना है तो आप बड़ी मुसीबत में पड़ सकते हैं। ओलेग ज़ेरेवेन्नी का जवाब बेहतर है, लेकिन उनके पास वोटों की एक छोटी संख्या है क्योंकि यह 2017 में पोस्ट किया गया था।
टोनी पुलोकास

115
var search = "world";
var replacement = "csharp";
string result = Regex.Replace(
    stringToLookInto,
    Regex.Escape(search), 
    replacement.Replace("$","$$"), 
    RegexOptions.IgnoreCase
);

Regex.Escape अगर आप उपयोगकर्ता इनपुट पर भरोसा करते हैं उपयोगी है जो शामिल कर सकते हैं Regex भाषा तत्वों

अपडेट करें

टिप्पणियों के लिए धन्यवाद, आपको वास्तव में प्रतिस्थापन स्ट्रिंग से बचने की ज़रूरत नहीं है।

यहाँ एक छोटी सी बेला है जो कोड का परीक्षण करती है :

using System;
using System.Text.RegularExpressions;           
public class Program
{
    public static void Main()
    {

        var tests = new[] {
            new { Input="abcdef", Search="abc", Replacement="xyz", Expected="xyzdef" },
            new { Input="ABCdef", Search="abc", Replacement="xyz", Expected="xyzdef" },
            new { Input="A*BCdef", Search="a*bc", Replacement="xyz", Expected="xyzdef" },
            new { Input="abcdef", Search="abc", Replacement="x*yz", Expected="x*yzdef" },       
            new { Input="abcdef", Search="abc", Replacement="$", Expected="$def" },
        };


        foreach(var test in tests){
            var result = ReplaceCaseInsensitive(test.Input, test.Search, test.Replacement);

            Console.WriteLine(
                "Success: {0}, Actual: {1}, {2}",
                result == test.Expected,
                result,
                test
            );

        }


    }

    private static string ReplaceCaseInsensitive(string input, string search, string replacement){
        string result = Regex.Replace(
            input,
            Regex.Escape(search), 
            replacement.Replace("$","$$"), 
            RegexOptions.IgnoreCase
        );
        return result;
    }
}

इसका आउटपुट है:

Success: True, Actual: xyzdef, { Input = abcdef, Search = abc, Replacement = xyz, Expected = xyzdef } 
Success: True, Actual: xyzdef, { Input = ABCdef, Search = abc, Replacement = xyz, Expected = xyzdef }
Success: True, Actual: xyzdef, { Input = A*BCdef, Search = a*bc, Replacement = xyz, Expected = xyzdef } 
Success: True, Actual: x*yzdef, { Input = abcdef, Search = abc, Replacement = x*yz, Expected = x*yzdef} 
Success: True, Actual: $def, { Input = abcdef, Search = abc, Replacement = $, Expected = $def }

2
यह विधि विफल हो जाती है यदि प्रतिस्थापन = "! @ # $% ^ & * ()" आप प्राप्त करें "! @ \ # $% \ ^ & * ()" के स्थान पर प्रतिस्थापित।
केकोडर

2
दूसरा Regex.Escapeखराब है, यह बैकस्लैश के साथ विशेष वर्णों को उपसर्ग करेगा। सबसे अच्छा तरीका है लगता है .Replace ("$", "$ $"), जो थोड़े गूंगा है ( stackoverflow.com/a/10078353 )।
डैनी टुप्पेनी

1
@dannyTuppeny: आप सही हैं ... मैंने तदनुसार उत्तर अपडेट किया
स्टीव बी

54

2.5X तेजी और अन्य प्रभावी तरीकों की तुलना में सबसे प्रभावी विधि:

/// <summary>
/// Returns a new string in which all occurrences of a specified string in the current instance are replaced with another 
/// specified string according the type of search to use for the specified string.
/// </summary>
/// <param name="str">The string performing the replace method.</param>
/// <param name="oldValue">The string to be replaced.</param>
/// <param name="newValue">The string replace all occurrences of <paramref name="oldValue"/>. 
/// If value is equal to <c>null</c>, than all occurrences of <paramref name="oldValue"/> will be removed from the <paramref name="str"/>.</param>
/// <param name="comparisonType">One of the enumeration values that specifies the rules for the search.</param>
/// <returns>A string that is equivalent to the current string except that all instances of <paramref name="oldValue"/> are replaced with <paramref name="newValue"/>. 
/// If <paramref name="oldValue"/> is not found in the current instance, the method returns the current instance unchanged.</returns>
[DebuggerStepThrough]
public static string Replace(this string str,
    string oldValue, string @newValue,
    StringComparison comparisonType)
{

    // Check inputs.
    if (str == null)
    {
        // Same as original .NET C# string.Replace behavior.
        throw new ArgumentNullException(nameof(str));
    }
    if (str.Length == 0)
    {
        // Same as original .NET C# string.Replace behavior.
        return str;
    }
    if (oldValue == null)
    {
        // Same as original .NET C# string.Replace behavior.
        throw new ArgumentNullException(nameof(oldValue));
    }
    if (oldValue.Length == 0)
    {
        // Same as original .NET C# string.Replace behavior.
        throw new ArgumentException("String cannot be of zero length.");
    }


    //if (oldValue.Equals(newValue, comparisonType))
    //{
    //This condition has no sense
    //It will prevent method from replacesing: "Example", "ExAmPlE", "EXAMPLE" to "example"
    //return str;
    //}



    // Prepare string builder for storing the processed string.
    // Note: StringBuilder has a better performance than String by 30-40%.
    StringBuilder resultStringBuilder = new StringBuilder(str.Length);



    // Analyze the replacement: replace or remove.
    bool isReplacementNullOrEmpty = string.IsNullOrEmpty(@newValue);



    // Replace all values.
    const int valueNotFound = -1;
    int foundAt;
    int startSearchFromIndex = 0;
    while ((foundAt = str.IndexOf(oldValue, startSearchFromIndex, comparisonType)) != valueNotFound)
    {

        // Append all characters until the found replacement.
        int @charsUntilReplacment = foundAt - startSearchFromIndex;
        bool isNothingToAppend = @charsUntilReplacment == 0;
        if (!isNothingToAppend)
        {
            resultStringBuilder.Append(str, startSearchFromIndex, @charsUntilReplacment);
        }



        // Process the replacement.
        if (!isReplacementNullOrEmpty)
        {
            resultStringBuilder.Append(@newValue);
        }


        // Prepare start index for the next search.
        // This needed to prevent infinite loop, otherwise method always start search 
        // from the start of the string. For example: if an oldValue == "EXAMPLE", newValue == "example"
        // and comparisonType == "any ignore case" will conquer to replacing:
        // "EXAMPLE" to "example" to "example" to "example" … infinite loop.
        startSearchFromIndex = foundAt + oldValue.Length;
        if (startSearchFromIndex == str.Length)
        {
            // It is end of the input string: no more space for the next search.
            // The input string ends with a value that has already been replaced. 
            // Therefore, the string builder with the result is complete and no further action is required.
            return resultStringBuilder.ToString();
        }
    }


    // Append the last part to the result.
    int @charsUntilStringEnd = str.Length - startSearchFromIndex;
    resultStringBuilder.Append(str, startSearchFromIndex, @charsUntilStringEnd);


    return resultStringBuilder.ToString();

}

नोट: मामले को अनदेखा करें == StringComparison.OrdinalIgnoreCaseपैरामीटर के लिए StringComparison comparisonType। यह सभी मूल्यों को बदलने के लिए सबसे तेज़, केस-असंवेदनशील तरीका है।


इस विधि के लाभ:

  • उच्च सीपीयू और मेमोरी दक्षता;
  • यह नियमित अभिव्यक्ति के साथ अन्य तरीकों की तुलना में 2.5 गुना तेज सबसे तेज समाधान है (अंत में सबूत);
  • इनपुट स्ट्रिंग (सेट से भागों को दूर करने के लिए उपयुक्त newValueकरने के लिए null), इस के लिए अनुकूलित;
  • मूल .NET C # string.Replace व्यवहार के समान, समान अपवाद;
  • अच्छी तरह से टिप्पणी की, समझने में आसान;
  • सरल - कोई नियमित अभिव्यक्ति नहीं। नियमित अभिव्यक्तियाँ हमेशा उनकी बहुमुखी प्रतिभा के कारण धीमी होती हैं (यहां तक ​​कि संकलित);
  • इस पद्धति का अच्छी तरह से परीक्षण किया गया है और अन्य समाधानों में अनंत लूप की तरह कोई छिपी हुई खामियां नहीं हैं, यहां तक ​​कि उच्च श्रेणी निर्धारण भी:

@AsValeO: रेगेक्स भाषा तत्वों के साथ काम नहीं करता है, इसलिए यह सार्वभौमिक तरीका नहीं है

@ माइक स्टिलियन: इस कोड के साथ एक समस्या है। यदि नया में पाठ पुराने में पाठ का सुपरसेट है, तो यह अंतहीन लूप का उत्पादन कर सकता है।


बेंचमार्क प्रूफ : यह समाधान @ रेक्टेव बी, कोड से रेगेक्स की तुलना में 2.59X गुना तेज है:

// Results:
// 1/2. Regular expression solution: 4486 milliseconds
// 2/2. Current solution: 1727 milliseconds — 2.59X times FASTER! than regex!

// Notes: the test was started 5 times, the result is an average; release build.

const int benchmarkIterations = 1000000;
const string sourceString = "aaaaddsdsdsdsdsd";
const string oldValue = "D";
const string newValue = "Fod";
long totalLenght = 0;

Stopwatch regexStopwatch = Stopwatch.StartNew();
string tempString1;
for (int i = 0; i < benchmarkIterations; i++)
{
    tempString1 = sourceString;
    tempString1 = ReplaceCaseInsensitive(tempString1, oldValue, newValue);

    totalLenght = totalLenght + tempString1.Length;
}
regexStopwatch.Stop();



Stopwatch currentSolutionStopwatch = Stopwatch.StartNew();
string tempString2;
for (int i = 0; i < benchmarkIterations; i++)
{
    tempString2 = sourceString;
    tempString2 = tempString2.Replace(oldValue, newValue,
        StringComparison.OrdinalIgnoreCase);

    totalLenght = totalLenght + tempString2.Length;
}
currentSolutionStopwatch.Stop();

मूल विचार - @ Darky711; धन्यवाद @MinerR के लिए StringBuilder


5
मुझे यकीन है कि आप एक स्ट्रिंग के बजाय स्ट्रिंग स्ट्रिंग का उपयोग करके इसे और भी तेज बना सकते हैं।
माइनर

1
@ मेन आप सही हैं, मैंने मूल रूप से अनंत लूप के बिना @ Darky711 समाधान को अपडेट किया है, इसलिए मैंने इसका उपयोग किया String। हालांकि, StringBuilderवास्तव में 30-40% की तुलना में तेजी है String। मैंने समाधान अपडेट कर दिया है। धन्यवाद;)
ओलेग ज़ेरेवेनैनी

2
दिलचस्प दृष्टिकोण। प्रदर्शन के मामले में संभवतः बेहतर एक (मेरा :) से बेहतर)। आम तौर पर एक साझा साझा पुस्तकालय में जोड़ने के लिए एक विधि।
स्टीव बी

2
'नेमॉफ' भावों का उपयोग केवल C # 6.0 और उससे आगे के लिए इसे वैध बनाता है। यदि आप VS2013 में हैं, तो आप केवल अपवादों में ऑपरेंड को हटाकर इसका उपयोग कर सकते हैं।
लंचपैड

बाहर टिप्पणी के लिए "// अगर (oldValue.Equals (newValue, comparType))" StringComparison.Ordinal के साथ तुलना को बदलें?
रोजर विलोक

31

एक्सटेंशन हमारे जीवन को आसान बनाते हैं:

static public class StringExtensions
{
    static public string ReplaceInsensitive(this string str, string from, string to)
    {
        str = Regex.Replace(str, from, to, RegexOptions.IgnoreCase);
        return str;
    }
}

10
और भागने से हमारा जीवन कम हो जाता है :-) वापसी Regex.Replace (इनपुट, Regex.Escape (खोज), प्रतिस्थापन .eplace ("$", "$$"), RegexOptions.IgnoreCase);
वामन

29

Regex का उपयोग करते हुए बहुत सारे सुझाव। इसके बिना इस विस्तार विधि के बारे में कैसे:

public static string Replace(this string str, string old, string @new, StringComparison comparison)
{
    @new = @new ?? "";
    if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(old) || old.Equals(@new, comparison))
        return str;
    int foundAt = 0;
    while ((foundAt = str.IndexOf(old, foundAt, comparison)) != -1)
    {
        str = str.Remove(foundAt, old.Length).Insert(foundAt, @new);
        foundAt += @new.Length;
    }
    return str;
}

ध्यान दें कि तुलनात्मक तर्क का उपयोग वास्तविक प्रतिस्थापन को करने के लिए नहीं किया जा रहा है (यह हमेशा असंवेदनशील होता है)
बोलो

2
इस कोड के साथ एक समस्या है। यदि नया में पाठ पुराने में पाठ का सुपरसेट है , तो यह अंतहीन लूप का उत्पादन कर सकता है। एक बार जब नई पर डाला जाता है foundat , का मूल्य foundat की लंबाई से उन्नत करने की जरूरत है नई
माइक स्टिलियन

comparisonपैरामीटर का उपयोग किया जाना चाहिए IndexOf, इसके बजायStringComparison.CurrentCultureIgnoreCase
Maxence

@ बेलो मैंने इसे तुलना तर्क का उपयोग करने के लिए संपादित किया है (हो सकता है कि इसकी समीक्षा की जाए)।
bradlis7

2
मैं नई स्ट्रिंग वापस करने के लिए इस स्थिति को भी अलग करूँगा: if(old.Equals(@new, comparison)) return @new;क्योंकि, नए स्ट्रिंग अपरकेस / लोअरकेस में भिन्न हो सकते हैं।
s --unıɐ ɐ qɐp

13

इस सहायक फ़ंक्शन को खोजने के लिए आप Microsoft.VisualBasic नाम स्थान का उपयोग कर सकते हैं :

Replace(sourceString, "replacethis", "withthis", , , CompareMethod.Text)

मुझे अपने उत्तर पर गर्व हुआ जब तक मैंने यह नहीं देखा कि यह एक बेहतर उत्तर है क्योंकि यह अंदर बनाया गया है। Ex: Strings.Replace ("TeStInG123", "t", "z", 1, -1, तुलना -Method.Text) रिटर्न zeSzInG123 "
बोलो

यदि स्ट्रिंग खोजा जा रहा है तो स्ट्रिंग एक रिक्त स्ट्रिंग है।
मफु जोश

1
4.7.2 नेट में, आपको यह काम करने के लिए Microsoft.VisualBasic का संदर्भ जोड़ना होगा। .Net कोर में, Microsoft.VisualBasic.Strings वर्ग (वैसे भी संस्करण 10.3.0 में) प्रतिस्थापित फ़ंक्शन को लागू करने के लिए प्रकट नहीं होता है। यह पॉवर्सशेल में तब भी काम करता है, जब आप ऐड-क्लास -न्यूजनाम Microsoft.VisualBasic पहले करते हैं।
प्रोफेसर वॉन लेमनगर्ल

6

( संपादित: 'नग्न लिंक' समस्या के बारे में पता नहीं था, इसके बारे में क्षमा करें)

यहाँ से लिया गया :

string myString = "find Me and replace ME";
string strReplace = "me";
myString = Regex.Replace(myString, "me", strReplace, RegexOptions.IgnoreCase);

लगता है कि आप मामले की असंवेदनशील स्ट्रिंग की कमी की शिकायत करने वाले पहले नहीं हैं।


5

संशोधित प्रकार में पास का उपयोग करने के लिए @ Darky711 का उत्तर दिया गया है और संभवत: यथासंभव नामकरण और xml टिप्पणियों के ढांचे को प्रतिस्थापित किया गया है।

/// <summary>
/// Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
/// </summary>
/// <param name="str">The string performing the replace method.</param>
/// <param name="oldValue">The string to be replaced.</param>
/// <param name="newValue">The string replace all occurrances of oldValue.</param>
/// <param name="comparisonType">Type of the comparison.</param>
/// <returns></returns>
public static string Replace(this string str, string oldValue, string @newValue, StringComparison comparisonType)
{
    @newValue = @newValue ?? string.Empty;
    if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(oldValue) || oldValue.Equals(@newValue, comparisonType))
    {
        return str;
    }
    int foundAt;
    while ((foundAt = str.IndexOf(oldValue, 0, comparisonType)) != -1)
    {
        str = str.Remove(foundAt, oldValue.Length).Insert(foundAt, @newValue);
    }
    return str;
}

2

मैंने विस्तार विधि लिखी है:

public static string ReplaceIgnoreCase(this string source, string oldVale, string newVale)
    {
        if (source.IsNullOrEmpty() || oldVale.IsNullOrEmpty())
            return source;

        var stringBuilder = new StringBuilder();
        string result = source;

        int index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);

        while (index >= 0)
        {
            if (index > 0)
                stringBuilder.Append(result.Substring(0, index));

            if (newVale.IsNullOrEmpty().IsNot())
                stringBuilder.Append(newVale);

            stringBuilder.Append(result.Substring(index + oldVale.Length));

            result = stringBuilder.ToString();

            index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
        }

        return result;
    }

मैं पिछले एक्सटेंशन विधि के लिए दो अतिरिक्त एक्सटेंशन विधियों का उपयोग करता हूं:

    public static bool IsNullOrEmpty(this string value)
    {
        return string.IsNullOrEmpty(value);
    }

    public static bool IsNot(this bool val)
    {
        return val == false;
    }

2
Upvoted। लेकिन IsNotएक्सटेंशन को गंभीरता से लिया जा रहा है :)
nawfal

निराशाजनक, यह सभी स्थितियों में काम नहीं करता है। मैं एक प्रतिष्ठित नाम से गुजर रहा था और यह तब तक लागू होता है जब तक कि स्ट्रिंग एक लाख वर्ण लंबा न हो जाए और फिर स्मृति से बाहर चला जाए
Bbb

वैकल्पिक के नीचे की पेशकश की है कि मेरे मुद्दे तय
Bbb

मुझे वास्तव में पसंद है.IsNot
ttugates

1

खोज स्ट्रिंग पर पेत्रुसिओ के उत्तर का विस्तार Regex.Escape, और स्टीव बी के उत्तर में सुझाए गए मिलान समूह से बचकर (और मेरे स्वाद में कुछ मामूली बदलाव):

public static class StringExtensions
{
    public static string ReplaceIgnoreCase(this string str, string from, string to)
    {
        return Regex.Replace(str, Regex.Escape(from), to.Replace("$", "$$"), RegexOptions.IgnoreCase);
    }
}

जो निम्नलिखित अपेक्षित परिणाम देगा:

Console.WriteLine("(heLLo) wOrld".ReplaceIgnoreCase("(hello) world", "Hi $1 Universe")); // Hi $1 Universe
Console.WriteLine("heLLo wOrld".ReplaceIgnoreCase("(hello) world", "Hi $1 Universe"));   // heLLo wOrld

हालाँकि, बिना प्रदर्शन के आप निम्नलिखित प्राप्त करेंगे, जो String.Replaceकि केवल मामले-असंवेदनशील से अपेक्षित व्यवहार नहीं है:

Console.WriteLine("(heLLo) wOrld".ReplaceIgnoreCase_NoEscaping("(hello) world", "Hi $1 Universe")); // (heLLo) wOrld
Console.WriteLine("heLLo wOrld".ReplaceIgnoreCase_NoEscaping("(hello) world", "Hi $1 Universe"));   // Hi heLLo Universe

1

यह काम नहीं करता है: मैं कुछ और बहुत जल्दी या आसान होने के लिए इमेजिंग नहीं कर सकता।

public static class ExtensionMethodsString
{
    public static string Replace(this String thisString, string oldValue, string newValue, StringComparison stringComparison)
    {
        string working = thisString;
        int index = working.IndexOf(oldValue, stringComparison);
        while (index != -1)
        {
            working = working.Remove(index, oldValue.Length);
            working = working.Insert(index, newValue);
            index = index + newValue.Length;
            index = working.IndexOf(oldValue, index, stringComparison);
        }
        return working;
    }
}

मुझे नहीं पता कि यह तेज़ है लेकिन यह संक्षिप्त है, रेगेक्स ओवरहेड और संभावित समस्याओं का उपयोग नहीं करता है और अंतर्निहित StringComparison का उपयोग करता है।
fvlinden

0

नीचे फ़ंक्शन स्ट्रिंग सेट से सभी मैच शब्द को (इस) को हटाने के लिए है। रविकांत सोनारे द्वारा।

private static void myfun()
{
    string mystring = "thiTHISThiss This THIS THis tThishiThiss. Box";
    var regex = new Regex("this", RegexOptions.IgnoreCase);
    mystring = regex.Replace(mystring, "");
    string[] str = mystring.Split(' ');
    for (int i = 0; i < str.Length; i++)
    {
        if (regex.IsMatch(str[i].ToString()))
        {
            mystring = mystring.Replace(str[i].ToString(), string.Empty);

        }
    }
    Console.WriteLine(mystring);
}

इस समारोह को स्ट्रिंग सेट से सभी स्ट्रिंग को प्रतिस्थापित किया जाता है ... रविकांत सोनारे,
रविकांत सोनारे

0

@Georgy Batalov समाधान का उपयोग करते समय मुझे निम्नलिखित उदाहरण का उपयोग करते समय एक समस्या थी

string original = "blah, DC = blh, DC = blih, DC = bloh, DC = com"; स्ट्रिंग को प्रतिस्थापित किया गया = मूल .eplaceIgnoreCase (", DC =", "" ")

नीचे बताया गया है कि कैसे मैंने उसका विस्तार रद्द किया

public static string ReplaceIgnoreCase(this string source, string oldVale, 
string newVale)
    {
        if (source.IsNullOrEmpty() || oldVale.IsNullOrEmpty())
            return source;

        var stringBuilder = new StringBuilder();
        string result = source;

        int index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
        bool initialRun = true;

        while (index >= 0)
        {
            string substr = result.Substring(0, index);
            substr = substr + newVale;
            result = result.Remove(0, index);
            result = result.Remove(0, oldVale.Length);

            stringBuilder.Append(substr);

            index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
        }

        if (result.Length > 0)
        {
            stringBuilder.Append(result);
        }

        return stringBuilder.ToString();
    }

0

नीचे स्ट्रिंग अनदेखा चरित्र मामले को बदलने का विकल्प है

String thisString = "hello world"; 
String replaceString = "World";

//thisString.Replace("World", "csharp"); 
//below is the alternative to replace string ignoring character case

int start = StringUtils.indexOfIgnoreCase(thisString,replaceString);
String searchKey = thisString.substring(start, start+replaceString.length());
thisString= thisString.replaceAll(searchKey ,replaceString );
System.out.println(thisString);

//prints hello World

0

आप Regexक्लास भी ट्राई कर सकते हैं ।

var regex = new Regex( "camel", RegexOptions.IgnoreCase ); var newSentence = regex.Replace( sentence, "horse" );


-3

मैं इसे पसंद करता हूं - "हैलो वर्ल्ड"। टावलर ()। रिप्लेसमेंट ("दुनिया", "सीएसएचआरपी");


1
यह सब कुछ कम कर देगा, यहां तक ​​कि ऐसे शब्द भी जिन्हें प्रतिस्थापित नहीं किया जाना चाहिए था।
जेजे

जाहिर है, आप इसका उपयोग केवल तभी कर सकते हैं जब आप मामले के बारे में परेशान न हों।
हर्षल
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.