Avatar of Cony TN
Cony TN
Flag for Germany

asked on 

Removing specific characters from string

Hi everyone,

I have a  string (Length = 10) in which the character '-1' could come at any position.
I would like to remove all occurences of '-1'.For example if I have
the following string:
string strWithMinusOne = "-1,2,3,-1,-1,105,-1,2,-1,-1" ;
//After removing all the '-1' I should have the following string
string result = "2,3,105,2"

Open in new window

I tried something like this:
public string RemoveAllOccurencesOfMinusOne(string strWithMinusOne, char s)
 //  strWithMinusOne =   "-1,2,3,-1,-1,105,-1,2,-1,-1" 
string resultString = string.Empty;
    foreach (char c in strWithMinusOne)
        if (c == '-1')
             index1 = strWithMinusOne.IndexOf(c);
             resultString = strWithMinusOne.Remove(index1, 1);
        return  resultString;

It didn't seem to work and I don't know how to handle the commas (,) ...

Could someone give a hint?
Thank you

Open in new window


Avatar of undefined
Last Comment

8/22/2022 - Mon