I have a textbox used to store comments in an ASP.NET application. When the user updates the database, the comment and the phrase “Last Updated By: [userName]” is inserted into the database.
What I would like to do is find the phrase “Last Updated By:” and remove it and all characters after it until the end of the string.
One more things, the userName is not always the same length.
Any ideas how I would do that?
Thanks again.
Chris
You can use the Substring method combined with the IndexOf method:
string comment = "This is random comment from database before clipping Last Updated By: NightStalker";
string clippedComment = comment.Substring(0, comment.IndexOf("Last Updated By:")).Trim();
This code will work, but it is obviously excluding any error handling which might be a good idea to add. To do some fact checking before executing this code is to move the IndexOf method to it’s own int
int endIndex = comment.IndexOf("Last Updated By:");
if (endIndex > 1)
{
string clippedComment = comment.Substring(0, endIndex).Trim();
}
I hope that helps