.split
Archives Forums/BlitzMax Bug Reports/.split
| ||
| Sorry if asked before etc. etc. Print Len("1;2;3;".split(";")) I expected 3, not 4. Yes, I can easily wipe the last ; .. but, dunno.. I expected 3, not 4. :) It would be more consistent, somehow. Think of script parsing where you make a consistency agreement to close each command with a semi colon. How about a check whether the last separator is followed by something or nothing? (max 1.34) |
| ||
| I prefer 4 here... It might be sometimes handy if it strips last null string for you. But what if the null part is important for someone else? :) |
| ||
| Then, what about: Print Len("1;2;3;".split(";",1)) where the ,1 means something like: "string ends with separator" |
| ||
| I find it more useful to have an empty string on the end for my purposes, so I suppose it's more useful to have extra information you can get rid of instead of dropping the empty string. Just write a little function to get it to do what you want, because the built-in split function's never going to change:
Function numbits(str$,separator$)
Local bits$[]=str.split(separator)
If bits[Len(bits)-1]=""
Return Len(bits)-1
Else
Return Len(bits)
EndIf
End Function
Print numbits("2;3;4;",";")
Print numbits("2;2;4",";")
|
| ||
| It seems to work exactly how I would expect it to. The result should always be "number of separators + 1". |
| ||
| Or, if you don't ever want the ending element, just call a function on the array every time you split a string that removes the last Null element. |
| ||
Or:
Function trimarray$[](arr$[])
n=Len(arr)
start=0
finish=n-1
While start<n And (Not arr[start])
start:+1
Wend
While finish>0 And (Not arr[finish])
finish:-1
Wend
Return arr[start..finish+1]
End Function
Print ",".join(trimarray(";1;;2;".split(";")))
|
| ||
| Yes, or all of them. |
| ||
| How about a check whether the last separator is followed by something or nothing? If it is a separator then it has to be followed by something. And the empty string is a valid string, so what appears to be nothing is in fact an empty string. |
| ||
| Like what Floyd said. |
| ||
| Current usage works for me. Actually it'd scupper my game proper if Mark changed its functionality, as I use the Split() method loads. |