I have the following string in C#,
my-new-field.js?role=admin
I want to remove everything after ? and then ? as well so that the resultant string would be my-new-field.js
My string may not contain ? every time so I need to check if the ? is present in the string or not and then only need to remove ? and everything after ?.
How should I do that?
>Solution :
Fiddle: https://dotnetfiddle.net/42kjwW
string test = "my-new-field.js?role=admin";
int position = test.IndexOf("?");
if(position >= 0) {
test = test.Substring(0,position);
}
So, substring will "cut" the string from the position 0 (very begining) to the first position of character ?