get next word in string
BlitzMax Forums/BlitzMax Beginners Area/get next word in string
| ||
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? |
| ||
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 |
| ||
great thanks, i was using a long convoluted method to compare arrays u r smart! |
| ||
would you know how to get the second word after the substring? |
| ||
|
| ||
thanks good oneSuperStrict 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 |
| ||
Ah, yeah, I'd completely forgotten about the split method |
| ||
Didn't even know about it. That'll help me out loads with something I'm currently doing. |
| ||
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" |
| ||
Split! Sounds handy. Where's is this documented? Thanks. |
| ||
Split! Sounds handy. Where's is this documented? Thanks. Under Language->strings |