truncate string to last full stop (within 160 chars)

Discussion in 'ASP .Net' started by Stimp, Oct 27, 2006.

  1. Stimp

    Stimp Guest

    Hi all,

    Having a bit of a headache trying to do this.

    I want to create a string with a total length of 160 characters.

    The first few characters must always be in the string.. say around 45
    characters.

    After this I want to append another string, description, which can be
    any number of characters long.

    I want to truncate this string so that when it is appended onto the
    other string it doesn't exceed 160 characters BUT I want it truncated
    to the nearest '.'.

    I've been messing around with substring but not much luck... anyone done
    this before?

    Cheers,
    Peter
    --

    fiddlewidawiddum
     
    Stimp, Oct 27, 2006
    #1
    1. Advertising

  2. I'm sure someone here can codesmith this a bit better for optimization, but
    perhaps this will lead you on the right path.

    I left the var names a bit more telling...

    string firstString = "Product Name Is Here And is Around 45 Characters
    Long.";
    int firstStringLength = firstString.Length;

    string secondString
    = "This is a long description. This long description is really
    long. " +
    "Boy this is a really long description. Is anyone still reading
    this really long description. " +
    "Hello out there, are you stil reading? Hello?";

    string cutoffString = secondString.Substring(0, 159 - firstStringLength);

    int lengthtoLastPeriod = cutoffString.LastIndexOf(".") + 1;

    string newString = firstString + " " + cutoffString.Substring(0,
    lengthtoLastPeriod);

    On the cutoffString, I read your post as if the TOTAL of string1 and string2
    could not exceed 160 characters. If that's not the case, remove the "- firstStringLength"
    part. :)

    Hope this helps!

    -dl

    --
    David Longnecker
    Web Developer
    http://blog.tiredstudent.com

    > Hi all,
    >
    > Having a bit of a headache trying to do this.
    >
    > I want to create a string with a total length of 160 characters.
    >
    > The first few characters must always be in the string.. say around 45
    > characters.
    >
    > After this I want to append another string, description, which can be
    > any number of characters long.
    >
    > I want to truncate this string so that when it is appended onto the
    > other string it doesn't exceed 160 characters BUT I want it truncated
    > to the nearest '.'.
    >
    > I've been messing around with substring but not much luck... anyone
    > done this before?
    >
    > Cheers,
    > Peter
     
    David R. Longnecker, Oct 27, 2006
    #2
    1. Advertising

  3. Stimp

    Stimp Guest

    On Fri, 27 Oct 2006 Stimp <> wrote:
    > Hi all,
    >
    > Having a bit of a headache trying to do this.
    >
    > I want to create a string with a total length of 160 characters.
    >
    > The first few characters must always be in the string.. say around 45
    > characters.
    >
    > After this I want to append another string, description, which can be
    > any number of characters long.
    >
    > I want to truncate this string so that when it is appended onto the
    > other string it doesn't exceed 160 characters BUT I want it truncated
    > to the nearest '.'.
    >
    > I've been messing around with substring but not much luck... anyone done
    > this before?
    >
    > Cheers,
    > Peter


    I got it in the end...

    Dim iCharactersAvailable As Integer = 160 - iTotalTextChars

    sDescription = sDescription.Substring(0, sDescription.LastIndexOf(".",
    iCharactersAvailable, iCharactersAvailable) + 1)


    --

    fiddlewidawiddum
     
    Stimp, Oct 27, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,290
    Tim Rentsch
    Sep 23, 2005
  2. pauld
    Replies:
    2
    Views:
    1,023
    pauld
    Jun 3, 2008
  3. Hongyu
    Replies:
    9
    Views:
    911
    James Kanze
    Aug 8, 2008
  4. MacRules
    Replies:
    2
    Views:
    313
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    230
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page