Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / String Function Library.

Author
Message
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 19th Mar 2007 10:11 Edited at: 19th Mar 2007 10:13
String Function Library


What is it?

This is a Function Library I wrote to make your lives easier and to make up for the lack of string commands in DBP. There are various functions to replace words inside of strings, or to find where a word is and lots more.

What Functions are there?

Explode - Explode a string into an array separated by characters.
GSub - Substitute any words/letters within your string with other ones.
Find - Find the position of a word/letter within your string.
Sub - Get the substring of your string, remove any unwanted characters within a range.
Rep - Repeat a string a designated amount.
Reverse - Reverse a string.
Add/Remove - Add or remove a designated amount of characters from a string where anywhere within it.
Trim - Trim any trailing white space from either end of your string.
File Type - Return the filetype from a path.
File Name - Return the filename from a path.
File Path - Return the file path from a path.

Examples?

yes the full source code to the library is below, and it comes with some basic examples showing you how to use the functions. If you use these functions in any of your projects please credit me.



jayell leedham
18
Years of Service
User Offline
Joined: 24th Jan 2006
Location: North Yorkshire
Posted: 24th Mar 2007 13:05
A great idea!
With acknowledgments to your library, here is a code snipit which extends your "Where am I hiding?" code to find the 2nd, 33d etc occurences of a string...
Happy coding.

Attachments

Login to view attachments
jayell leedham
18
Years of Service
User Offline
Joined: 24th Jan 2006
Location: North Yorkshire
Posted: 25th Mar 2007 00:42
OOps - didn't do the "code snippet properly. Think I know howto now:-
ch$="DFRT Hopbbb Hopvvv Hop Hop"
returnn=String_Find(ch$,"Hop",4)
print "position of 4th occurence of Hop is ",returnn
wait key
end
Function String_Find( Lib_Input_String As String , Lib_Find_String As String, Occurence )
Check=0
Local Lib_Return_Value As Integer
If Lib_Find_String = ""
ExitFunction 0
Endif
For Lib_Scan = 1+Check To Len( Lib_Input_String )
If Mid$( Lib_Input_String , Lib_Scan ) = Mid$( Lib_Find_String , 1 )
If Len( Lib_Find_String ) > 1
For Lib_Search = 1 To Len( Lib_Find_String ) - 1
If Mid$( Lib_Input_String , Lib_Scan + Lib_Search ) = Mid$( Lib_Find_String , Lib_Search + 1 )
If Lib_Search = Len( Lib_Find_String ) - 1
if Check=occurence-1
ExitFunction Lib_Scan
else
inc Check
exit
endif
Endif
endif
Next Lib_Search
Else
ExitFunction Lib_Scan
Endif
Endif
Next Lib_Scan
EndFunction 0
jayell leedham
18
Years of Service
User Offline
Joined: 24th Jan 2006
Location: North Yorkshire
Posted: 15th Apr 2007 20:31 Edited at: 21st Apr 2007 22:36
A better, "tidied-up" version is:-

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 21st Apr 2007 22:32 Edited at: 21st Apr 2007 22:41
Have you seen my collection of string functions? It's been around a good few years now, but has some functions that you don't have.

If you like, you could combine any of mine that you don't have into yours.

Instr() Returns the numeric character position of a substring in a string.
Midstr() Returns a section of a string based on it's position and length.
MirrorStr() Reverses the characters in a string. "ABC" becomes "CBA".
TrimStr() Removes all leading and trailing spaces from a string.
LeftTrimStr() Removes all leading spaces from the beginning of a string.
RightTrimStr() Removes all trailing spaces from the end of a string.
AcronymStr() Turns a sentence into it's capitalised acronym. "three letter abbreviation" becomes "TLA".
LCapStr() Turns the words in a sentence capitalised. "one for the road" becomes "One For The Road".
ReplaceStr() Searches for a substring within a string and replaces it with a second substring.
RemoveStr() Searches for and removes a substring within a string.
RemoveSpcStr() Searches for and removes all multiple spaces and replaces them with a single space.
GetWordStr() Returns the nth word in a string.
WordCount() Returns the number of words in a string.
MakeStr() Creates a string containing x number of occurrences of a given string.
SortStr() Rearranges the characters in a string alphabetically.
ArrayStr() Parses the words in a string into a string array and returns the number of array elements used.
ColorText() Prints a string in multiple colours using HTML-style tags.

jayell leedham:

Doesn't it look even tidier when you use the code boxes? Just highlight all of your code and click on the Code button at the top.

I've done it for you in your last post - is it not working for you?

TDK_Man

Attachments

Login to view attachments
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 22nd Apr 2007 02:16
TDK, Would I be able to utilize your string functions for use in my Keyboard Input Handler. The KBIH is posted here in the code snippets, and eventually I will submit it to the codebase (free for anyone to use).

Also, essence may use some/all of the code from KBIH in his MirEVU framework. (just trying to cover all bases)

If you can elaborate:
How optimized are the string functions? Do you use recursion? Cause right now, I'm using brute force methods?

Is it part of a dll or can I just include the source code?

Thanks
~zenassem
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 22nd Apr 2007 04:50 Edited at: 22nd Apr 2007 04:52
Quote: "How optimized are the string functions? Do you use recursion?"


Not very optimized lol! They were written over five years ago. However, there's not a lot to optimize to be honest...

Quote: "Is it part of a dll or can I just include the source code?"


They are just source code functions. The zip file is attached to my above post - feel free to use as you wish if they are of any use.

TDK_Man

Login to post a reply

Server time is: 2024-11-22 18:47:04
Your offset time is: 2024-11-22 18:47:04