Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing everything after a character, including that character in C#

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading