i want to remove last word from a string after a particular character"-"
my string="Chain - 99.6"
i want to remove - 99.6 from given string.
how to delete string after a particular character ex:"-"
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satish BhatPosted Dec 2, 2011, 11:45 PM
string oldString = "Chain - 99.6";
int lastIndex = oldString.LastIndexOf("-");
string newString = oldString;
if (lastIndex != -1)
{
newString = oldString.Remove(lastIndex).Trim();
}
Response.Write(newString);
Datta KharadPosted Dec 3, 2011, 2:50 AM
string oldString="Chain -99.6";
if(oldString.Contains("-"))
{
string newString = oldString.Substring(0,oldString.LastIndexOf("-")).Trim();
}
Result:- Chain
Satish BhatPosted Dec 3, 2011, 1:45 AM
Datta KharadPosted Dec 3, 2011, 12:56 AM
You can use same result in single line:-
string oldString="Chain -99.6";
string newString = oldString.Substring(0,oldString.LastIndexOf("-")).Trim();
Result:- Chain