Getting one word of a string?

BlitzPlus Forums/BlitzPlus Beginners Area/Getting one word of a string?

neos300(Posted 2009) [#1]
how would i go about taking a 5 word string, putting the first word into another string, and the other words into another string, without knowing the length of the words?


GfK(Posted 2009) [#2]
You could do that in a second in blitzmax, with String.Split().

In Blitzplus you'll have to manually find the spaces using Instr(). You can then use the result of Instr() with Mid() to return the whole word into a new string. Can't write an example as I'm out of touch with anything prior to Blitzmax, but that's the science behind it.


xlsior(Posted 2009) [#3]
As long as you know that there are always multiple words and that there is always a space between them, you can do something like this:

mystring$="This string has five words"

firstspace=Instr(mystring," ",1)

firstword$=Left(mystring$,firstspace-1)
restword$=Mid$(mystring$,firstspace+1)

Print "The First:"+firstword
Print "The Rest :"+restword
WaitKey()


(This will also strip the space between the first and second word)

or without using the extra variables, duplicating the string search:
mystring$="This string has five words"
Print "The First word:"+Left(mystring$,Instr(mystring," ",1)-1)
Print "The Rest      :"+Mid$(mystring$,Instr(mystring," ",1)+1)



neos300(Posted 2009) [#4]
Ok, thanks.