Posted: 6th Oct 2002 5:54
Someone made a really cool string library, and that was on the old forums! Does anyone have it on hand, or does the creator?
Posted: 6th Oct 2002 15:09
Do you mean TDK_Man's?

That's on the RGT forums, but it's being updated at the moment so here is the code for the include file, no manual I'm afraid.

+ Code Snippet
Rem *** Include File: strfunc.dba ***
Rem By TDK_Man
Rem Created: 22/09/2002 10:58:55

Rem Included in Project: C:\Program Files\Dark Basic Software\Dark Basic Professional\Projects\Spelling\spellcheck.dbpro

Rem Dark Basic String Function Collection (c) TDK_Man September 2002
Rem Free to use - a mention would be nice if you do use it, but not compulsory

Function Instr(MainStr$,Search$,StartPos)
  Rem Instr Function By TDK_Man

  Rem Usage Examples:
  Rem NumVar=Instr("Main String","SubString",StartPos)
  Rem NumVar=Instr(Main$,Search$,StartPos)
  Rem Print Instr("The Rain In Spain","Rain",1)

  Rem Description:
  Rem INSTR is a function giving you the INSTRing capabilities supplied with many
  Rem versions of BASIC. Given the string which you are searching, a substring to
  Rem search for and the position within the main string to start, INSTR returns
  Rem an integer value for the start position of the substring in the main string.

  Rem For example, INSTR("ABCDEFGHIJK","DEF",1) will return 4 as the 'D' of 'DEF'
  Rem is found starting at the 4th character in the string being searched. The value
  Rem 0 is returned if the substring is not found.

  N=StartPos-1: StartFound=0: FoundPos=0
  Repeat
    Inc N
    If Mid$(MainStr$,N)=Left$(Search$,1)
      StartFound=N
    Else
      StartFound=0
    Endif

    If StartFound>0
      Build$=""
      For N2=StartFound To StartFound+(Len(Search$)-1)
        Build$=Build$+Mid$(MainStr$,N2)
      Next N2
      If Build$=Search$
        FoundPos=StartFound
      Endif
    Endif
  Until Build$=Search$ or N=Len(MainStr$)
EndFunction FoundPos


Function MidStr(MainStr$,Start,SLen)
  Rem MidStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=Midstr("Main String",StartPos,NumChars)
  Rem StringVar=Midstr(Main$,StartPos,NumChars)
  Rem Print Midstr("The Rain In Spain",5,4)

  Rem Description:
  Rem MIDSTR is a function giving you the proper multiple character returning MIDSTRing
  Rem (MID$) capabilities supplied with many versions of BASIC. Given the string which
  Rem you are searching, a start position and the number of characters to extract, MIDSTR
  Rem returns a substring of characters from the main string.

  Rem For example, MIDSTR("ABCDEFGHIJK",4,3) will return the string 'DEF' - being the
  Rem 3 characters in the main string starting at the 4th character.

  Rem If the supplied value 'Numchars' exceeds the length of the main string, then only
  Rem the remaining characters are returned.

  Build$=""
  For N=Start To Start+SLen-1
    Build$=Build$+Mid$(MainStr$,N)
  Next N
EndFunction Build$


Function MirrorStr(MainStr$)
  Rem MirrorStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=MirrorStr(Main$)
  Rem Print MirrorStr("The Rain In Spain")

  Rem Description:
  Rem MIRRORSTR is a function which returns a string with the original strings' contents
  Rem mirrored (reversed). No parameters are required other than the string to mirror.

  Rem For example, MIRRORSTR("ABCDEFGHIJK") will return the string 'KJIHGFEDCBA'.

  Build$=""
  For N=Len(MainStr$) To 1 Step-1
    Build$=Build$+Mid$(MainStr$,N)
  Next N
EndFunction Build$


Function TrimStr(MainStr$)
  Rem TrimStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=TrimStr(Main$)
  Rem Print TrimStr("   The Rain In Spain  ")

  Rem Description:
  Rem TRIMSTR is a function which returns a string with the original string's
  Rem leading and trailing spaces removed (trimmed). No parameters are required other
  Rem than the string to trim.

  Rem For example, TRIMSTR("    ABC  ") will return the string 'ABC'.

  Build$="": N=1
  While Mid$(MainStr$,N)=" "
    Inc N
  EndWhile
  Spos=N
  N=Len(MainStr$)
  While Mid$(MainStr$,N)=" "
    Dec N
  EndWhile
  Epos=N
  For N=Spos To Epos
    Build$=Build$+Mid$(MainStr$,N)
  Next N
EndFunction Build$


Function LeftTrimStr(MainStr$)
  Rem LeftTrimStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=LeftTrimStr(Main$)
  Rem Print LeftTrimStr("   The Rain In Spain  ")

  Rem Description:
  Rem LEFTTRIMSTR is a function which returns a string with the original string's
  Rem leading spaces removed (trimmed). No parameters are required other than
  Rem the string to trim.

  Rem For example, LEFTTRIMSTR("    ABC  ") will return the string 'ABC  '.

  Build$="": N=1
  While Mid$(MainStr$,N)=" "
    Inc N
  EndWhile
  For N2=N To Len(MainStr$)
    Build$=Build$+Mid$(MainStr$,N2)
  Next N2
EndFunction Build$


Function RightTrimStr(MainStr$)
  Rem RightTrimStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=RightTrimStr(Main$)
  Rem Print RightTrimStr("   The Rain In Spain  ")

  Rem Description:
  Rem RIGHTTRIMSTR is a function which returns a string with the original string's
  Rem trailing spaces removed (trimmed). No parameters are required other than
  Rem the string to trim.

  Rem For example, RIGHTTRIMSTR("    ABC  ") will return the string '    ABC'.

  Build$="": N=Len(MainStr$)
  While Mid$(MainStr$,N)=" "
    Dec N
  EndWhile
  For N2=1 To N
    Build$=Build$+Mid$(MainStr$,N2)
  Next N2
EndFunction Build$


Function AcronymStr(MainStr$)
  Rem AcronymStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=AcronymStr(Main$)
  Rem Print AcronymStr("this is a lower case sentence")

  Rem Description:
  Rem ACRONYMSTR is a function which returns the capitalised initals from the words
  Rem in the original sentence.

  Rem For example, ACRONYMSTR("beginners all-purpose symbolic instruction code") will return 'BASIC'.

  Build$=Upper$(Mid$(MainStr$,1))
  For N=2 To Len(MainStr$)
    If Mid$(MainStr$,N-1)=" "
      Build$=Build$+Upper$(Mid$(MainStr$,N))
    Endif
  Next N
EndFunction Build$


Function LCapStr(MainStr$)
  Rem LCapStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=LCapStr(Main$)
  Rem Print LCapStr("this is a lower case sentence")

  Rem Description:
  Rem LCAPSTR is a function which returns the original string's words with leading
  Rem capitals. No parameters are required other than the string to capitalise.

  Rem For example, LCAPSTR("this is lower case") will return 'This Is Lower Case'.

  Build$=Upper$(Mid$(MainStr$,1))
  For N=2 To Len(MainStr$)
    If Mid$(MainStr$,N-1)=" "
      Build$=Build$+Upper$(Mid$(MainStr$,N))
    Else
      Build$=Build$+Mid$(MainStr$,N)
    Endif
  Next N
EndFunction Build$


Function ReplaceStr(MainStr$,Substr1$,Substr2$)
  Rem ReplaceStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=ReplaceStr(Main$,find$,replace$)
  Rem Print ReplaceStr("one two three","two","six")

  Rem Description:
  Rem REPLACESTR replaces all occurences of the first substring with the second in the main string.
  Rem If substr1 does not exist in the main string then the function returns an empty string ("").

  Rem For example, REPLACESTR("Here is a red book", "red", "green") will return 'Here is a green book'.

  Build$=""
  Repeat
    Spos=Instr(MainStr$,Substr1$,1)
    LineBegin$=Left$(MainStr$,Spos-1)
    X=Len(MainStr$)-Spos-Len(Substr1$)
    LineEnd$=Right$(MainStr$,X+1)
    If Spos>0
      Build$=LineBegin$ + Substr2$ + LineEnd$
    Endif
  Until Spos=0
EndFunction Build$


Function WordCount(MainStr$)
  Rem WordCount Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=WordCount(Main$)
  Rem Print WordCount("Count these words please")

  Rem Description:
  Rem WORDCOUNT simply returns the number of words in the supplied string, assuming that the
  Rem string contains no leading or trailing spaces. Use TRIMSTR if necessary to remove them
  Rem before calling this function.

  Rem For example, WORDCOUNT("The cat sat on the mat") will return the value 6.

  Words=1
  For N=1 To Len(MainStr$)
    If Mid$(MainStr$,N)=" " Then Inc Words
  Next N
EndFunction Words


Function MakeStr(String$,NumCount)
  Rem MakeStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=MakeStr(String$,NumCount)
  Rem Print MakeStr("X",32)

  Rem Description:
  Rem MAKESTR creates a string containing 'NumCount' occurences of String$. Note that
  Rem the value of NumCount does not automatically represent the length of the newly
  Rem created string as String$ can be greater than 1 character in size.

  Rem For example, MAKESTR("0",8) will create the string '00000000' - Length 8.
  Rem and MAKESTR("ABC",4) will create the string 'ABCABCABCABC' - Length NOT 4!

  Build$=""
  For N=1 To NumCount
    Build$=Build$+String$
  Next N
EndFunction Build$


Function SortStr(String$,Case)
  Rem SortStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=SortStr(String$)
  Rem Print SortStr("The Cat Sat On the Mat")

  Rem Description:
  Rem SORTSTR creates a string containing all the characters in String$ organised
  Rem into alphabetical order, ignoring all spaces. If 'Case' is set to 0 then upper
  Rem and lower case are treated the same. If 'Case' is set to 1 then capitals and lower
  Rem case are sorted separately in their ASCII value order.

  Rem For example, SORTSTR("Hello World") will create the string 'deHllloorW' and
  Rem SORTSTR("FEACBD") will create the string 'ABCDEF'.

  StrLen=Len(String$)
  Dim Array(StrLen)
  For N=1 To StrLen
    If Mid$(String$,N)<>" " Then Array(N)=Asc(Mid$(String$,N))
  Next N
  Repeat
    Swap=0
    For N=1 To StrLen-1
      Rem a=97 (-32)
      If Case=0
        C1=Array(N): If C1<97 Then C1=C1+32
        C2=Array(N+1): If C2<97 Then C2=C2+32
      Else
        C1=Array(N): C2=Array(N+1)
      Endif
      If C1>C2
        Temp=Array(N)
        Array(N)=Array(N+1)
        Array(N+1)=Temp
        Swap=1
      Endif
    Next N
  Until Swap=0
  Build$=""
  For N=1 To StrLen
    Build$=Build$+Chr$(Array(N))
  Next N
  UnDim Array()
EndFunction Build$


Function RemoveStr(MainStr$,Substr1$)
  Rem RemoveStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=RemoveStr(Main$,find$)
  Rem Print RemoveStr("one two three","two ")

  Rem Description:
  Rem REMOVESTR removes the first occurence of the substr1 from the main string.
  Rem If words are being removed from a sentence, you must remember to include the space.

  Rem For example, REMOVESTR("Here is a red book", "red ") will return 'Here is a book'.

  Build$=""
  Repeat
    Spos=Instr(MainStr$,Substr1$,1)
    LineBegin$=Left$(MainStr$,Spos-1)
    X=Len(MainStr$)-Spos-Len(Substr1$)
    LineEnd$=Right$(MainStr$,X+1)
    If Spos>0
      Build$=LineBegin$ + LineEnd$
    Endif
  Until Spos=0
EndFunction Build$


Function RemoveSpcStr(MainStr$)
  Rem RemoveSpcStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=RemoveSpcStr(Main$)
  Rem Print RemoveSpcStr("one     two     three")

  Rem Description:
  Rem REMOVESPCSTR removes all multiple occurences of spaces from the main string and
  Rem replaces them with a single one. For example:
  Rem REMOVESPCSTR("I got     a    duff keyboard") will return 'I got a duff keyboard'.

  Build$=""
  For N=1 To Len(MainStr$)
    Build$=Build$+Mid$(MainStr$,N)
    If Mid$(MainStr$,N)=" "
      If Mid$(MainStr$,N+1)=" "
        Repeat
          Inc N
        Until Mid$(MainStr$,N)<>" "
        Dec N
      Endif
    Endif
  Next N
EndFunction Build$


Function GetWordStr(MainStr$,WordNum)
  Rem GetWordStr Function By TDK_Man

  Rem Usage Examples:
  Rem StringVar=GetWordStr(Main$,N)
  Rem Print GetWordStr("one two three",2)

  Rem Description:
  Rem GETWORDSTR returns the nth word from the supplied main string.
  Rem If n is gretaer than the number of words in the string, a NULL string ("") is returned.

  Rem For example, GETWORDSTR("Here is a red book",4) will return 'red'.

  Words=1
  For N=1 To Len(MainStr$)
    If Mid$(MainStr$,N)=" " Then Inc Words
  Next N

  If WordNum<=Words
    Spos=1: WordCount=0: Build$=""
    For N=1 To Len(MainStr$)
      If Mid$(MainStr$,N)=" "
        For N2=Spos to N-1
          B$=B$+Mid$(MainStr$,N2)
        Next N2
        Inc WordCount
        If WordCount=WordNum
          Build$=B$
          N=Len(MainStr$)+1
        Else
          B$=""
          Spos=N+1
        Endif
      Endif
      If N=Len(MainStr$)
        For N2=Spos to N
          B$=B$+Mid$(MainStr$,N2)
        Next N2
        Inc WordCount
        If WordCount=WordNum
          Build$=B$
        Endif
      Endif
    Next N
  Else
    Build$=""
  Endif
EndFunction Build$
Posted: 21st Oct 2002 1:35
The Instr function doesnt seem to work - it only finds the substring I supply for some mainstrings - does anyone have any ideas why ?
Posted: 25th Oct 2002 19:31
I don't know, it's not mine but I don't think it's DBP compatible.
Posted: 20th Nov 2002 0:41
Great news!!! (For me anyway) - I've got DB Pro on the way!

When I get it, the String Function Library V1.0 will become V1.1 with some bug fixes and cool new string functions (like ReplaceChar and wordwrapping).

Can't wait for the postman to call...

TDK