Page 1 of 1

seperating words

Posted: Sun Jan 15, 2006 2:47 am
by guest
is there a command that seperates induvidual words in a string?

Posted: Sun Jan 15, 2006 6:52 am
by MystikShadows
Hi there Guest ;-).

There's no functions that does that per se no. However, here's a small function that will do that for you.

Code: Select all

' ========================================================
'  NAME........: GetWordNumber()
'  PARAMETERS..: WorkString AS STRING
'                WordNumber AS LONG
'  RETURN......: "" Or the desired word Number
'  ASSUMES.....: WorkString has contents, WordNumber > 0
'  CALLED FROM.: Anywhere
' --------------------------------------------------------
'  DESCRIPTION.: This function accepts a string and a
'                Word Number as paremeters, it then finds
'                the given word number and returns it to
'                the calling function.
' ========================================================
FUNCTION GetWordNumber(WorkString AS STRING, WordNumber AS LONG) AS STRING

    ' ----------------
    '  Work Variables
    ' ----------------
    DIM  Counter      AS LONG
    DIM  WordCounter  AS LONG
    DIM  Position     AS LONG
    DIM  Skipper      AS LONG
    DIM  ReturnString AS STRING

    ' ----------------------
    '  Initialize Variables
    ' ----------------------
    WordCounter  = 1
    Position     = 1
    ReturnString = ""
    ' ---------------------------------------------------------
    '  First we put ourselves at the beginning of the 1st word
    ' ---------------------------------------------------------
    IF MID$(WorkString, Position, 1) = " " THEN
       DO WHILE MID$(WorkString, Position, 1) = " "
          Position = Position + 1
       LOOP
    END IF
    ' --------------------------------------
    '  Then we start the Word Count Process
    ' --------------------------------------
    FOR Counter = Position TO LEN(WorkString)
        IF MID$(WorkString, Counter, 1) = " " THEN
           WordCounter = WordCounter + 1
           DO WHILE MID$(WorkString, Counter, 1) = " "
              Counter = Counter + 1
           LOOP
        END IF
        ' ----------------------------------------------------------
        '  If it's the wanted word number we put it in ReturnString
        ' ----------------------------------------------------------
        IF WordCounter = WordNumber THEN
           IF MID$(WorkString, Counter, 1) <> " " THEN
              ReturnString = ReturnString + MID$(WorkString, Counter, 1)
           END IF
        END IF
    NEXT Counter
    ' ---------------------------------
    '  Return the word in ReturnString
    ' ---------------------------------
    GetWordNumber = ReturnString

END FUNCTION
Hope this helps.