Let's say I have a string:
"C:\Program Files (x86)\Steam\steam.exe /lets go 342131 some random text"
And I want to remove from that string the 'steam.exe' and everything that follows after that. So my trimmed string would look like:
"C:\Program Files (x86)\Steam\"
How can I do that in C#?
Simply use IndexOf
and Substring
methods:
int index = str.IndexOf("steam.exe");
string result = str.Substring(0, index);
If you want to remove something from the end of a string use String.Remove
:
int indexOfSteam = text.IndexOf("steam.exe");
if(indexOfSteam >= 0)
text = text.Remove(indexOfSteam);
It's the same as text.Substring(0, indexOfSteam)
. It just makes the intention clearer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With