How to split a string
BlitzPlus Forums/BlitzPlus Beginners Area/How to split a string
| ||
How to split a string? The string is a record and contains ascii 31 as a field delimiter. Each record is containing a name and a number; both strings of non fixed length. I may add another field. So lets consider 3 fields instead of just 2. |
| ||
There are several entries for this in the code archives - here's one: http://www.blitzbasic.com/codearcs/codearcs.php?code=2991 (this is one of those things people love to keep remaking and re-adding - search the "Miscellaneous" and "Algorithm" sections for "split" and you'll find lots more examples) |
| ||
Be careful with some of those code archives. ...some of them dont always behave as expected with consecutive delimiters...ie empty fields. |
| ||
OK Thanks! |
| ||
How i would do it, in words : count the number of characters in the instring (the "length" of the string) each character has a position in the string from 1 to count (this can be from 0 to count-1 in some others langages) create an array to store each outstring the current outstring id is 1 for each pos (from 1 to count) read the character at this pos in the instring if the character does not correspond to the character you use for a delimiter add this char to the current outstring elseif the character corresponds to the character you use for a delimiter create another outstring the current outstring id is id+1 (do not add this char to the current outstring) useful functions : Len() Mid() |
| ||
Thank You. |