Remove last character from a string if it contains last character as "," or ";" - C#

Ankaprasad
Posted by Ankaprasad under C# category on | Points: 40 | Views : 346
Input : str = "AML,Bladder Cancer,";
 
public static string RemoveLastCharacter(string str) //str = "AML,Bladder Cancer,"
{
if (string.IsNullOrEmpty(str)) return "";
if (str.EndsWith(","))
{
str = str.Substring(0, str.Length - 1);
}
else if (str.EndsWith(";"))
{
str = str.Substring(0, str.Length - 1);
}
return str; // "AML,Bladder Cancer"
}


Result : "AML,Bladder Cancer"

Comments or Responses

Login to post response