Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Need help truncating strings
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 24 January 2008 :  05:28:32  Show Profile
My brainbox is refusing to engage this morning; I know I can write these myself 'cause I know I've written something similar before, just can't find them!

What I need are two scripts that will truncate a string in different ways.

The first needs to truncate it to x characters but, if character x isn't a space, snip it at the last space before that character.

The second need to drop everything after the second occurrence of a substring within the string.

Anyone with a fully functioning brain who could take a look for me, please?


Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 24 January 2008 :  05:40:42  Show Profile  Send ruirib a Yahoo! Message
What happens if there is no second substring in the second script?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 24 January 2008 :  05:44:17  Show Profile
Ah, should've explained that:

If there is a second occurrence of the substring and if there's anything in the string after that then it should be truncated, if not then the original string should be returned. The strings that will be getting passed in this case, though, will always have at least 4 occurrences of the substring, which is closing paragraph tag.


Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 24 January 2008 :  05:45:23  Show Profile  Send ruirib a Yahoo! Message
Ok, I'm having a look. Trying to code the first one now.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 24 January 2008 :  05:48:18  Show Profile  Send ruirib a Yahoo! Message
Should have asked about the first too - if there is no space before the last X chars, what to return?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 24 January 2008 :  06:02:27  Show Profile
See, like I said, brainbox not engaged! If there's no space at or before character x then just snip at character x, whatever it may be.


Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 24 January 2008 :  06:19:03  Show Profile  Send ruirib a Yahoo! Message
Still the first script - stumbling on the case where you have multiple spaces ending the truncated string... Of course, in that case, you just want to get rid of all spaces, right?


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 24 January 2008 :  06:32:55  Show Profile
Multiple consecutive spaces won't be an issue, the string is being passed through another function that takes care of them.

And the truncated string will be trimmed before being displayed so trailing spaces there won't be an issue either.

To use the title of this topic as an example:

- If I wanted to trim it to 10 characters that would be the second space so it should become "Need help".

- If I wanted to trim it to 15 characters that would be the c in "truncating" so it should become "Need help" too.

- If there were no spaces in that string and I wanted to trim it to 15 characters, it should become "Needhelptruncat".


Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 24 January 2008 :  06:48:57  Show Profile  Send ruirib a Yahoo! Message
Had to delete it, will post it back in a sec.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 24 January 2008 :  07:05:03  Show Profile
No worries, Rui, it worked a treat But as I was about to thank you, the brain had it's first spark of thought for the day and came up with this:
function cut(s,c)
	if len(s)<c then
		cut=s
		exit function
	end if
	cut=left(s,c)
	if instr(cut," ")>0 and right(cut,1)<>" " then cut=left(cut,instrrev(cut," "))
end function

Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”

Edited by - Shaggy on 24 January 2008 07:05:21
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 24 January 2008 :  07:10:12  Show Profile  Send ruirib a Yahoo! Message
Lol, as long as it works for you, but what I posted wasn't working. As you will trim it, you can afford to stop at the first space from the end ;).


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 24 January 2008 :  10:04:24  Show Profile
Here's what I have so far for the second one:
function snip(s,c)
	snip=instr(s,c)
	snip=left(s,instr(right(s,len(s)-snip),c)+snip+len(c)-1)
end function
It works fine even if it's not that pretty.


Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 24 January 2008 :  10:12:38  Show Profile  Send ruirib a Yahoo! Message
It's compact enough and that usually doesn't look pretty.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 24 January 2008 :  10:21:35  Show Profile
Tends to be the case with most of my functions!


Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.38 seconds. Powered By: Snitz Forums 2000 Version 3.4.07