0

I have a string that could look like this: smithj_Website1 or it could look like this rodgersk_Website5 etc, etc. I want to be able to store in a string what is after the "_". So IE (Website1, Website5,..)

Thanks

Josh
  • 1,813
  • 4
  • 26
  • 33

2 Answers2

7

Should be a simple as using substring

string mystr = "test_Website1"
string theend = mystr.SubString(mystr.IndexOf("_") + 1)
// theend = "Website1"

mystr.IndexOf("_") will get the position of the _ and adding one to it will get the index of the first character after it. Then don't pass in a second parameter and it will automatically take the substring starting at the character after the _ and stopping and the end of the string.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Bob Fincheimer
  • 17,978
  • 1
  • 29
  • 54
3
int startingIndex = inputstring.IndexOf("_") + 1;
string webSite = inputstring.Substring(startingIndex);

or, in one line:

string webSite = inputstring.Substring(inputstring.IndexOf("_") + 1);
AllenG
  • 8,112
  • 29
  • 40