Replace first comma in C #

I am trying to find a way in C # to replace the first occurrence of a regular expression in a string.

So if string = ",1,2,3,4,5", I want to do something like

string = replacefirst(",","")

to always give the result:

string = "1,2,3,4,5"
+3
source share
6 answers

You can use Trim for this:

var myTrimmedString = myString.TrimStart(',');
+11
source
string s = "a;b;c;";
Regex rx = new Regex(";");
string s2 = rx.Replace(s, "", 1);  //"ab;c;"
+7
source

In the regular expression, “^” means “beginning”

Regex.Replace(",1,2,3,4,5", "^,", "")

gives 1,2,3,4,5

+5
source

What about:

if(yourString.Startswith(','))
      yourString = yourString.Substring(1);
+1
source

Change the regular expression so that it matches only the semicolon.

0
source
string MyString = ",1,2,3,4,6";
MessageBox.Show(MyString.Substring(1, MyString.Length-1));
0
source

Source: https://habr.com/ru/post/1731996/


All Articles