Home > Mobile >  how do i remove a string from another string in c# unity
how do i remove a string from another string in c# unity

Time:12-16

I have two strings (text) and (a). I want to remove (a) from (text) after an if statement this is what I have

public string text; 
public string a = 'a';

void Update(){
     if(text.Contains('a')){
        text - a;
     }
}

but I get error with this that I cannot use - how do I remove it

CodePudding user response:

public static IEnumerable<int> AllIndexesOf(this string str, string value) {
        for (int index = 0;; index  = value.Length) {
            index = str.IndexOf(value, index);
            if (index == -1)
                break;
            yield return index;
        }
    }

for check:

string str = "but I get error with this that I cannot use - how do I remove it";
        string remove = "e";
        foreach (var start in str.AllIndexesOf(remove).Reverse())
        {
            str = str.Remove(start, 1);
        }
        Console.WriteLine(str);

CodePudding user response:

A simple way of doing this is using the string.Replace

string text = "This is my text";
string a = "i";
text = text.Replace(a, "II");

'text' will be: ThIIs IIs my text

  • Related