get next word in string

BlitzMax Forums/BlitzMax Beginners Area/get next word in string

slenkar(Posted 2008) [#1]
If I had a string:

"my birthday is tomorrow"

and a substring "my birthday is"

how could I get the next word after "my birthday is"?


you cant just subtract the strings from one another because the sentence could be longer:

e.g. "my birthday is tomorrow said bill"

and also the sentence could have different words on the front

e.g.

"and he said my birthday is tomorrow"


you can turn the string into an array of words

e.g.
the_string$="my birthday is tomorrow
local temp_array$[]=the_String.split(" ")

temp_array[0]="my"
temp_array[1]="birthday"

if that helps?


Perturbatio(Posted 2008) [#2]
Global haystack:String = "did you know my birthday is tomorrow said bill"
Global needle:String = "my birthday is "


Local pos:Int = haystack.find(needle)
Local remainder:String = haystack[ pos + Len(needle)..]
Local birthday:String = remainder[0.. remainder.find(" ")]

Print birthday




slenkar(Posted 2008) [#3]
great thanks,
i was using a long convoluted method to compare arrays

u r smart!


slenkar(Posted 2008) [#4]
would you know how to get the second word after the substring?


Perturbatio(Posted 2008) [#5]



slenkar(Posted 2008) [#6]
thanks good one

SuperStrict

Global haystack:String = "did you know my birthday is tomorrow said bill"
Global needle:String = "my birthday is "


Local pos:Int = haystack.find(needle)
Local remainder:String = haystack[ pos + Len(needle)..]

Local words:String[] = remainder.Split(" ") 

Print words[0]
Print words[1]


shortened it a bit


Perturbatio(Posted 2008) [#7]
Ah, yeah, I'd completely forgotten about the split method


GfK(Posted 2008) [#8]
Didn't even know about it. That'll help me out loads with something I'm currently doing.


dmaz(Posted 2008) [#9]
just a little shorter again :)
Local birthday:String = remainder.Split(" ")[0]
or just 
Print remainder.Split(" ")[0]

it would be nice if bmax had built in regex so you could do
Print remainder.Split("\s+")[0] 

because split fails if there is 2 spaces or a tab after "is"


Grey Alien(Posted 2008) [#10]
Split! Sounds handy. Where's is this documented? Thanks.


Perturbatio(Posted 2008) [#11]
Split! Sounds handy. Where's is this documented? Thanks.


Under Language->strings