forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


String-manipulate functions...

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  

Do you think there must be more string-manipulate functions in VDS???
Yes
55%
 55%  [ 5 ]
Yes, but till then I will use (sample) scripts to do it.
44%
 44%  [ 4 ]
No, there are some dll's to do that, so I don't need that in VDS.
0%
 0%  [ 0 ]
No
0%
 0%  [ 0 ]
Total Votes : 9

Author Message
Skit3000
Admin Team


Joined: 11 May 2002
Posts: 2166
Location: The Netherlands

PostPosted: Sun Sep 15, 2002 1:17 pm    Post subject: String-manipulate functions... Reply with quote

Hello,

Cause I think VDS misses some major string-manipulate functions, I've
made a script with all I can think of... There are 13 in total. If someone
has onother one, or onother idea, please post a reply...

[code:1:9828b27d0a]
rem Use this fieldseparate character for all functions...
rem All the examples use a pipe character ( | )
%%fieldsep = |

rem Sometimes, a script will only replace or delete a parameter once.
rem If you want to delete al, use something like this:
rem %%string = Hello, this is a string... Isn't it?
rem %%delete = is
rem repeat
rem %%string = %%delete|%%string
rem gosub Deletestring
rem until @equal(@pos(%%delete,%%string),0)

%%string = %%parameter1|%%parameter2|%%parameter3|%%string
gosub What-do-you-wanna-do?

:Invertcase
rem This will invert the characters from upper- to lowercase and viseversa
rem Usage:
rem %%string = Hello how are you?
rem gosub Invertcase
rem The new string will be in %%string

%%var1 = 1
%%var2 =
repeat
if @both(@greater(@asc(@substr(%%string,%%var1,%%var1)),64),@greater(91,@asc(@substr(%%string,%%var1,%%var1))))
%%var2 = %%var2@lower(@substr(%%string,%%var1,%%var1))
else
%%var2 = %%var2@upper(@substr(%%string,%%var1,%%var1))
end
%%var1 = @succ(%%var1)
until @equal(@pred(%%var1),@len(%%string))
%%string = %%var1
exit


:Sentencecasestring
rem This will make the first character in a sentence a capital one.
rem It makes no difference if you use a @cr(), a dot, ?, : or !
rem Usage:
rem %%string = hello how are you?@cr() i am fine. thank you! i want to say something: hi!
rem gosub Sentencecasestring

%%var1 =
%%string = @upper(@substr(%%string,1,1))@substr(%%string,2,@len(%%string))
repeat
if @equal(@substr(%%string,%%var1,%%var1),@chr(46))@equal(@substr(%%string,%%var1,%%var1),@chr(58))@equal(@substr(%%string,%%var1,%%var1),@chr(63))@equal(@substr(%%string,%%var1,%%var1),@chr(33))
%%var2 = @substr(%%string,@fadd(%%var1,2),@fadd(%%var1,2))
%%string = @strdel(%%string,@fadd(%%var1,2),@fadd(%%var1,2))
%%string = @strins(%%string,@fadd(%%var1,2),@upper(%%var2))
end
if @equal(@substr(%%string,%%var1,%%var1),@cr())
%%var2 = @substr(%%string,@fadd(%%var1,2),@fadd(%%var1,2))
%%string = @strdel(%%string,@fadd(%%var1,2),@fadd(%%var1,2))
%%string = @strins(%%string,@fadd(%%var1,2),@upper(%%var2))
end
%%var1 = @succ(%%var1)
until @equal(@len(%%string),%%var1)
exit


:Titlecasestring
rem This will make the first character of a word capital.
rem Usage:
rem %%string = This is a string
rem gosub Titlecasestring
rem %%string now contains This Is A String

if @not(@equal(@pos(@chr(32),%%string),0))
%%var1 =
repeat
%%var1 = %%var1@upper(@substr(@substr(%%string,1,1)))@substr(%%string,2,@pos(@chr(32),%%string))
%%string = @trim(@strdel(%%string,1,@pos(@chr(32),%%string)))
until @equal(@pos(@chr(32),%%string),0)
end
%%string = %%var1@upper(@substr(%%string,1,1))@substr(%%string,2,@len(%%string))
exit


:Countwordsinstring
rem This counts the words in a string.
rem Only a words between a space are counted.
rem Usage:
rem %%string = This is a string
rem gosub Countwordsinstring
rem It will return the number of words in %%string

%%var1 = 0
repeat
if @not(@equal(@pos(@chr(32),%%string),0))
%%var1 = @succ(%%var1)
end
%%string = @trim(@strdel(%%string,1,@pos(@chr(32),%%string)))
until @equal(%%string,)
if @not(@equal(%%var1,0))
%%string = @succ(%%var1)
end
exit


:Wordericstring
rem This will make a string 'Worderic', just like @numeric()
rem Usage:
rem %%string = Hello. My phonenumber is 0123456789
rem gosub Wordericstring
rem It will return 'Hello My phonenumber is' into %%string

%%var1 = 0
repeat
if @both(@greater(@asc(@substr(%%string,%%var1,%%var1)),64),@greater(91,@asc(@substr(%%string,%%var1,%%var1))))@both(@greater(@asc(@substr(%%string,%%var1,%%var1)),96),@greater(123,@asc(@substr(%%string,%%var1,%%var1))))@equal(@asc(@substr(%%string,%%var1,%%var1)),32)
%%var1 = @succ(%%var1)
else
%%string = @strdel(%%string,%%var1,%%var1)
end
until @equal(%%var1,@succ(@len(%%string)))
exit


:Valueinstring
rem This checks if a string is in a list.
rem Usage:
rem %%string = Hello|Hi|How are you|Hello|Morning|Hello
rem gosub Valueinstring
rem The first parameter is the word to search for.
rem If you search for 'Hel', you'll find nothing, cause it is case extensive.
rem Returns null if no matches are found.

option fieldsep,%%fieldsep
parse "%%var1",%%string
%%string = @strdel(%%string,1,@succ(@len(%%var1)))
%%var3 = 0
%%var4 =
repeat
parse "%%var2",%%string
%%string = @strdel(%%string,1,@succ(@len(%%var2)))
%%var3 = @succ(%%var3)
if @both(@equal(%%var2,%%var1),@equal(@len(%%var2),@len(%%var1)))
if @not(@equal(%%var4,))
%%var4 = %%var4|%%var3
else
%%var4 = %%var3
end
end
until @equal(%%string,)
if @equal(%%var4,)
%%var4 = 0
else
%%string = %%var4
end
exit


:Overlaystring
rem This will overlay one string over an other.
rem Usage:
rem %%string = Hello World!!!|All|7|5
rem gosub Overlaystring
rem 'Hello World!!!' is the first string.
rem 'All' is the string which must be over the first.
rem The '7' is the place from where the second string must overlay the first
rem The '5' is the length that must be overlayed...
rem You can use @len(secondstring) OR put a number in it by yourself...

option fieldsep,%%fieldsep
parse "%%var1;%%var2;%%var3;%%var4",%%string
if @not(@equal(@len(%%var2),%%var4))
repeat
%%var2 = %%var2" "
until @equal(@len(%%var2),%%var4)
end
%%string = @strins(@strdel(%%var1,%%var3,@pred(@fadd(%%var3,%%var4))),%%var3,%%var2)
exit

:Lowestvalue
rem This will give you the lowest number in a row
rem Usage:
rem %%string = 1|6|3|5
rem gosub Lowestvalue
rem This will return '1' in %%string

option fieldsep,%%fieldsep
parse "%%var2",%%string
repeat
parse "%%var1",%%string
%%string = @strdel(%%string,1,@fadd(@len(%%var1),1))
if @greater(%%var2,%%var1)
%%var2 = %%var1
end
until @equal(%%string,)
%%string = %%var2
exit


:Highestvalue
rem This will give you the highest number in a row
rem Usage:
rem %%string = 1|6|3|5
rem gosub Highestvalue
rem This will return '6' in %%string

option fieldsep,%%fieldsep
repeat
parse "%%var1",%%string
%%string = @strdel(%%string,1,@fadd(@len(%%var1),1))
if @greater(%%var1,%%var2)
%%var2 = %%var1
end
until @equal(%%string,)
%%string = %%var2
exit


:Padstring
rem With this you can Pad a string to the left or the right.
rem Usage:
rem %%string = Right|7|x|Hello
rem gosub Padstring
rem You can use 'Right' or "Left'
rem The '7' is the length that the string must be
rem The 'x' is the character that fills up the empty spaces.
rem The output will be in %%string

option fieldsep,%%fieldsep
parse "%%var1;%%var2;%%var3",%%string
%%string = @strdel(%%string,1,@fadd(@fadd(@fadd(@len(%%var1),@len(%%var2)),@len(%%var3)),3))
if @not(@greater(@len(%%string),%%var2))
repeat
if @equal(%%var1,Left)
%%string = %%var3%%string
else
%%string = %%string%%var3
end
until @equal(@len(%%string),%%var2)
end
exit


:Spacestring
rem This will place s p a c e s in your string
rem Usage:
rem %%string = Hello world
rem gosub Spacestring
rem %%string will contain the 'spaced' word... :-)

%%var1 = 0
repeat
%%string = @strins(%%string,%%var1,@chr(32))
%%var1 = @fadd(%%var1,2)
until @greater(%%var1,@len(%%string))
exit


:Strangestring
rem This will make your string like StRiNg...
rem Usage:
rem %%string = Hello world
rem gosub Strangestring
rem %%string will be the new string

%%string = @lower(%%string)
%%var1 = 1
repeat
%%string = @strins(@strdel(%%string,%%var1,%%var1),%%var1,@upper(@substr(%%string,%%var1,%%var1)))
%%var1 = @fadd(%%var1,2)
until @greater(%%var1,@len(%%string))
exit


:Replacestring
rem This will replace the first parameter by the second
rem Usage:
rem %%string = Hello|Hi|Hello World
rem gosub Replacestring
rem %%string wil contain the new string
rem Note that only the first time the parameter will be found, the string will be replaced.

option fieldsep,%%fieldsep
parse "%%var1;%%var2",%%string
%%string = @strdel(%%string,1,@fadd(@fadd(@len(%%var1),@len(%%var2)),2))
%%string = @strins(@strdel(%%string,@pos(%%var1,%%string),@pred(@fadd(@pos(%%var1,%%string),@len(%%var1)))),@pos(%%var1,%%string),%%var2)
exit


:Deletestring
rem This will delete the first parameter out of the second
rem Usage:
rem %%string = Hello|Hello World
rem gosub Deletestring
rem %%string wil contain the new string
rem Note that only the first time the parameter will be found, the string will be deleted.

option fieldsep,%%fieldsep
parse "%%var1",%%string
%%string = @strdel(%%string,1,@succ(@len(%%var1)))
%%string = @strdel(%%string,@pos(%%var1,%%string),@fadd(@pos(%%var1,%%string),@len(%%var1)))
exit


:Rightsearchstring
rem This is the same as @pos(), except this is from the right...
rem Usage:
rem %%string = Hello|Hello world
rem gosub Rightsearchstring
rem Hello is the string to search for, Hello world is the string to search in...
rem The output will be in %%string

option fieldsep,%%fieldsep
parse "%%var1",%%string
%%var2 = @strdel(%%string,1,@succ(@len(%%var1)))
%%var3 =
repeat
%%var3 = %%var3@substr(%%var1,@len(%%var1),@len(%%var1))
%%var1 = @strdel(%%var1,@len(%%var1),@len(%%var1))
until @equal(%%var1,)
%%var4 =
repeat
%%var4 = %%var4@substr(%%var2,@len(%%var2),@len(%%var2))
%%var2 = @strdel(%%var2,@len(%%var2),@len(%%var2))
until @equal(%%var2,)
%%string = @fadd(@pos(%%var3,%%var4),@len(%%var3))
exit


:Reversestring
rem This will reverse %%string
rem Usage:
rem %%string = Hello world
rem gosub Reversestring

%%var1 = %%string
%%string =
repeat
%%string = %%string@substr(%%var1,@len(%%var1),@len(%%var1))
%%var1 = @strdel(%%var1,@len(%%var1),@len(%%var1))
until @equal(%%var1,)
exit


:Copystring
rem This will return the string X times after eighother, 12345123451234512345
rem Usage:
rem %%string = 4|12345
rem gosub Copystring
rem The 4 stands for the times that it should be repeated.
rem The output will be in %%string

option fieldsep,%%fieldsep
parse "%%var1",%%string
%%var2 = @strdel(%%string,1,@succ(@len(%%var1)))
%%string =
repeat
%%string = %%string%%var2
%%var1 = @pred(%%var1)
until @equal(%%var1,0)
exit[/code]

EDIT1: Added four new functions: Invertcase, Sentencecasestring, Titlecasestring & Countwordsinstring.


Last edited by Skit3000 on Fri Sep 20, 2002 7:14 pm; edited 1 time in total
Back to top
View user's profile Send private message
Dr. Dread
Professional Member
Professional Member


Joined: 03 Aug 2001
Posts: 1065
Location: Copenhagen, Denmark

PostPosted: Tue Sep 17, 2002 11:19 pm    Post subject: Reply with quote

Hey

I miss better string handling functions in VDS, too! So I have also build myself kind of a string handling library like yours. I posted some
of these routines a couple of weeks ago.

http://www.vdsworld.com/forum/viewtopic.php?t=635 - Trimleft & Trimright
http://www.vdsworld.com/forum/viewtopic.php?t=636 - Padleft & Padright
http://www.vdsworld.com/forum/viewtopic.php?t=641 - Count + last position

Some of these are covered by your examples, as well.

Greetz
Dr. Dread

_________________
~~ Alcohol and calculus don't mix... Don't drink and derive! ~~

String.DLL * advanced string processing
Back to top
View user's profile Send private message
Skit3000
Admin Team


Joined: 11 May 2002
Posts: 2166
Location: The Netherlands

PostPosted: Wed Sep 18, 2002 1:42 pm    Post subject: Reply with quote

Hi Dr. Dread,

I knew that you posted that, but it were only a few examples. That's why I have 'coverd' a few of your functions, so that they were all together...

Do you maybe have some other ideas??
Back to top
View user's profile Send private message
Dr. Dread
Professional Member
Professional Member


Joined: 03 Aug 2001
Posts: 1065
Location: Copenhagen, Denmark

PostPosted: Wed Sep 18, 2002 2:39 pm    Post subject: Reply with quote

skit3000 wrote:
Hi Dr. Dread,

I knew that you posted that, but it were only a few examples. That's why I have 'coverd' a few of your functions, so that they were all together...

Do you maybe have some other ideas??


Yo, skit!

I just didn't know whether you had seen those posts or not. And I think that it's a good idea to post other ways of achieving the same targets.
Then we could take them for a spin and perhaps find out which ones are the best - maybe it would be a good idea to build some kind of an
actual string handling library that could be used for all sorts of string processing needs.

And yes, I may have for other string handling stuff - and also character operations. I may be able to find a couple in some old progs
and surely we can think of other ones that may come in handy.

Greetz
Dread

_________________
~~ Alcohol and calculus don't mix... Don't drink and derive! ~~

String.DLL * advanced string processing
Back to top
View user's profile Send private message
Dr. Dread
Professional Member
Professional Member


Joined: 03 Aug 2001
Posts: 1065
Location: Copenhagen, Denmark

PostPosted: Thu Sep 19, 2002 9:55 am    Post subject: Reply with quote

OK, as promised I did a bit of brainstorming as concerns string processing/evaluating routines that could be useful:

* count words in string
* title case (capitalize first char in each word in upper case)
* sentence case (first word in each sentence in upper)
* invert case
* test for lower case chars
* test for upper case chars
* test for alphanumeric chars
* test for alphabetic chars
* test for whitespace
* remove whitespace (mid-string, not just trim)
* Boolean searches (AND/OR/NOT)
* regular expression searches (probably not viable in VDS)
* case-sensitive search&replace
* search&replace - match whole word only
* find doublets in lists

I'm starting out now on the ones typed in bold. I'll post in the VDS3 source code section. Stay tuned Wink

Greetz
Dread

_________________
~~ Alcohol and calculus don't mix... Don't drink and derive! ~~

String.DLL * advanced string processing


Last edited by Dr. Dread on Fri Sep 20, 2002 8:07 am; edited 1 time in total
Back to top
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Sep 19, 2002 6:56 pm    Post subject: Reply with quote

Hey Dread, Smile

Here's an example of the alpha-numeric method I use. It
can easily be modified to get only alpha or only numeric...
Code:

%s = "0123456789abcdefghijklmnopqrstuvwxyz"

%i = @input(Enter something...)

if %i
  %x = 1
  REPEAT
    if @greater(@pos(@substr(%i, %x), %s), 0)
       %a = %a@substr(%i, %x)
    else
       %n = %n@substr(%i, %x)
    end
    %x = @succ(%x)
  UNTIL @greater(%x, @len(%i))
  INFO Alpha-numeric:@cr()@cr()%a@cr()@cr()@cr()Not alpha-numeric:@cr()@cr()%n
end

Cheers, Mac Smile

_________________
VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Back to top
View user's profile Send private message Send e-mail
Hortalonus
Valued Contributor
Valued Contributor


Joined: 15 Mar 2002
Posts: 344
Location: Western USA

PostPosted: Thu Sep 19, 2002 7:21 pm    Post subject: Reply with quote

Many thanks for all of the string handling routines! Very Happy
Back to top
View user's profile Send private message Send e-mail
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Thu Sep 19, 2002 7:37 pm    Post subject: Reply with quote

And here's an UPPER/LOWER case example that checks for alpha...
__________________________________________________________________________________________________________________________
Code:

%s = "abcdefghijklmnopqrstuvwxyz"

%i = @input(Enter something...)

if %i
  %x = 1
  REPEAT
    if @greater(@pos(@substr(%i, %x), %s), 0)
       %%pos = @pos(@substr(%i, %x), %s)
       if @equal(@substr(%i, %x), @substr(%s, %%pos), EXACT)
          %l = %l@substr(%i, %x)
       else
          %u = %u@substr(%i, %x)
       end
    else
       %n = %n@substr(%i, %x)
    end
    %x = @succ(%x)
  UNTIL @greater(%x, @len(%i))
  INFO String: %i@tab()@cr()@cr()Upper case: %u@cr()@cr()Lower case: %l@cr()@cr()Not alpha: %n
end

Cheers, Mac Smile

_________________
VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Back to top
View user's profile Send private message Send e-mail
Dr. Dread
Professional Member
Professional Member


Joined: 03 Aug 2001
Posts: 1065
Location: Copenhagen, Denmark

PostPosted: Fri Sep 20, 2002 7:15 am    Post subject: Reply with quote

Great, Mac! Smile

I grabbed the general idea for this. I will elaborate a bit on it (support for extended chars etc.) and make an alpha/numeric routine.

Greetz
Dr. Dread

_________________
~~ Alcohol and calculus don't mix... Don't drink and derive! ~~

String.DLL * advanced string processing
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group