Counting Characters and then replacing end with trailing periods
If I were to only allow a certain amount of characters in a string, how would I go about replacing the last word with trailing periods(...)?
This might be a good solution:
Let's say your string is called "oldStr", and you allow 100 characters:
// retrieve the first 100 characters
string newStr = oldStr.Substring(0,100);
// finds the last space
int lastSpace = newStr.LastIndexOf(" ");
/* removes the last characters after the last space, in case the last space wasn't exactly at the end of a word */
if (lastSpace < 99)
newStr = newStr.Substring(0,lastSpace-1)
// add "..." at the end
newStr += "..."
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)