Author |
Topic |
|
Shaggy
Support Moderator
Ireland
6780 Posts |
Posted - 24 January 2008 : 05:28:32
|
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 |
|
Shaggy
Support Moderator
Ireland
6780 Posts |
Posted - 24 January 2008 : 05:44:17
|
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.” |
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
|
Shaggy
Support Moderator
Ireland
6780 Posts |
Posted - 24 January 2008 : 06:02:27
|
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.” |
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
Posted - 24 January 2008 : 06:19:03
|
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 |
|
|
Shaggy
Support Moderator
Ireland
6780 Posts |
Posted - 24 January 2008 : 06:32:55
|
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.” |
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
|
Shaggy
Support Moderator
Ireland
6780 Posts |
Posted - 24 January 2008 : 07:05:03
|
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 |
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
|
Shaggy
Support Moderator
Ireland
6780 Posts |
Posted - 24 January 2008 : 10:04:24
|
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.” |
|
|
ruirib
Snitz Forums Admin
Portugal
26364 Posts |
|
Shaggy
Support Moderator
Ireland
6780 Posts |
Posted - 24 January 2008 : 10:21:35
|
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.” |
|
|
|
Topic |
|