[Gambas-user] bug in function split?

ron ronstk at ...239...
Tue Apr 6 07:19:40 CEST 2004


On Tuesday 06 April 2004 06:31, Frank Berg wrote:
> hi
>
> an call like s=split("1 2 3 4"," ") has as result :     s[0]=1
>                                                                        
> s[1]=2 ....
>
> an call like s=split("1   2    3    4") hast as result s[0]=1
>                                                                        
> s[1]= s[2]= s[3]=2.....
>
> i mean it is not correct..
>
> an call like s=split("1   2    3    4") must be in result     s[0]=1
>                                                                            
>     s[1]=2 s[2]=3 s[3]=4....
>
> frank

Sorry but you are partial wrong.
By default, the comma character is the separator, and there are no escape 
characters. (From the Help)

The default split is 1 character.

If you change it for easy reading the space to a '+'
then for s=split("1++2+++3++4","+") you should get

s[0]=1
s[1]=
s[2]=2
s[3]=
s[4]=
s[5]=3

Between the first + and the second + IS nothing and is in this way correct.
I understand your mistake but you have to see the separator in exact way

The bug you mention can be right but is not the one you say.
And the result is as you say with the arrays is.

If the comma is the default and in all your examples is no comma.
Then you should have a array of only 1 argument, the whole string.
for the last two

I verify this and it show that the to last examples are as I say
a array of 1 element containing the whole string.
The forth is with a space as separator and result is correct.

 PUBLIC SUB Main2()
DIM s AS String[]

s=Split("1 2 3 4 5"," ")
show(s)

s=Split("1  2    3   4   5")
show(s)

s=Split("1  2    3   4   5")
show(s)

s=Split("1  2   3  4   5"," ")
show(s)

END

SUB show(s AS String[])
  
  DIM i AS Integer
  FOR i=0 TO s.count-1
  PRINT i,s[i]
  NEXT
  PRINT "---------------"
END

result in
0	1
1	2
2	3
3	4
4	5
---------------
0	1  2    3   4   5
---------------
0	1  2    3   4   5
---------------
0	1
1	
2	2
3	
4	
5	3
6	
7	4
8	
9	
10	5
---------------











More information about the User mailing list