www.webdeveloper.com
Results 1 to 2 of 2

Thread: Counting Characters and then replacing end with trailing periods

  1. #1
    Join Date
    Jul 2004
    Posts
    61

    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(...)?

  2. #2
    Join Date
    Oct 2004
    Posts
    34
    Hi,
    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 += "..."

    Good Luck

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles