Removing part of a string - C#

Asked

Viewed 4,227 times

4

How can I remove my last characters from a string before reaching a certain character? For example:

string url = "www.google.com/test";

I needed to remove the test word until I reached the "/" character. I would like to play the rest in another variable, just like this:

string result = "www.google.com";

Is that even possible? Thanks in advance!

1 answer

4


One option is to use Split.

string url = "www.google.com/teste";
string resultado = url.Split('/')[0];


Or better than that, use the Substring.

string resultado = url.Substring(0,url.IndexOf('/'));
  • 3

    Perfect friend, that’s exactly what I needed! I’ll just wait 5 minutes to approve your answer.. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.