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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satish BhatRank #2,496 · 563 reputationPosted Dec 2, 2011, 6:45 PM EST
string oldString = "Chain - 99.6";
int lastIndex = oldString.LastIndexOf("-");
string newString = oldString;
if (lastIndex != -1)
{
newString = oldString.Remove(lastIndex).Trim();
}
Response.Write(newString);
Datta KharadRank #2,135 · 668 reputationPosted Dec 2, 2011, 7:56 PM EST
You can use same result in single line:-
string oldString="Chain -99.6";
string newString = oldString.Substring(0,oldString.LastIndexOf("-")).Trim();
Result:- Chain
Satish BhatRank #2,496 · 563 reputationPosted Dec 2, 2011, 8:45 PM EST
Datta KharadRank #2,135 · 668 reputationPosted Dec 2, 2011, 9:50 PM EST
string oldString="Chain -99.6";
if(oldString.Contains("-"))
{
string newString = oldString.Substring(0,oldString.LastIndexOf("-")).Trim();
}
Result:- Chain