Link to home
Start Free TrialLog in
Avatar of brihol44
brihol44

asked on

How can I cut off the last bit of a string that is X amount of characters and add "..." in it's place? Except on a space value.

How can I cut off the last bit of a string that is X amount of characters and add "..." in it's place? Except on a space value.

So...

If I have "The fox jumped over the fe..."

But not adding "..." on a space value like so...

If I have "The fox jumped over the ..."

Thanks,

Brian
Avatar of RickEpnet
RickEpnet
Flag of United States of America image

This will give you the first 15 and then add ...

<cfset mytring = mid(mytring,1,15) & "...">
What do you mean "Except on a space value"?
Avatar of brihol44
brihol44

ASKER

I would like it so it doesn't add it automatically at the end but only add it if it exceeds 15 characters. I guess the " " (space value) wouldn't count anyways.

Thx!
And I did just notice it adds it on a space character...

So it looks like it will add "..." on a space value. For example...

The fox jumped over ..." instead of...

The fox jumped over... Which is what I would like but I'm not sure if it's possible.

Thx!
Avatar of gdemaria
You want to make sure you get the full word, not cut the word in half, right?

But that means you will have less than 15 characters... you didn't say that you could round-UP to MORE than 15 characters...


<cfset  newString  =  reverse(listRest(reverse(left(theString,15))," .,;")) & "...">

That would be nice but the script above cuts all of the strings no matter if the String already fits within the space. Basically...

If the string is greater than X characters add the script. If not do nothing. The script above is cutting off the last word even if the the string fits nicely within the space.

Brian
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!
Depending on what you're doing you might also find these functions useful. If not for this task, they might come in handy in the future:

FullLeft
An enhanced version of left() that doesn't cut words off in the middle; instead, it searches backward until it finds a full word.

FormatterTeaser:
This function allows you to display n number of characters from a string with out cutting of in the middle of the word. The function will display a link at the end of the string if it is longer than the number of characters desired and the optional url argument is not empty.

I was wondering you gave gdemaria full credit because he added an "IF" statement. No offence to gdemaria But I truly feel the points should have been split. I am not one to normally complained that this seem pretty obvious. The whetever you feel is fair is fine.
Sorry, I'm not sure what the rules are. I just gave the points to the provider of the solution I used. I'll be sure to be more careful next time though and I thank you for your help. Again, sorry....

Brian
Brihol44, I think the solution was ID 37232937 because it included the part where words are not chopped in half (ending at the end of a whole word).   The CFIF was a follow-up to that solution.

The left() function you indicated in the first post is correct except for the word chopping part, and I'm ok with shared points with that, no problem.
There are no rules. He is right I missed his other comment.