integer to string

Is there an easy way to create a string of an integer array?
(Other than reading all items in array , converting each to string , and then concatenate.)

This does not work:

DIm s as string
s = join( str(numbers(),";")

if array integer contents is ; 1,2,3,4
the string should be “1,2,3,4”

DIm s as string s = join( str(numbers),";")
one parenthesis too much ?

Jean-Yves
This does not work either:

Parameter “fields” expects type String(), but this is type String.
s = join( str(numbers),";")

how is “numbers” declared ?
it seems to be a String but it should be an array of strings ?

Xojo doesn’t allow this kind of type conversion. You have to do it manually. You can add a global method for it:

Public Function str(n() as integer) as string()
  dim s() as string
  for each i as integer in n()
    s.append str( i )
  next
  return s()
End Function

Then this code will work.

dim numbers() as integer = Array( 1,2,3,4 ) dim s as string = join( str( numbers() ), ";" ) MsgBox s

But keep in mind, it’s not fast.

EDIT: improved the function using for each.

Depends a bit on what you want to do with the string of numbers.
In addition to Aaron solution there is also the possibility to use JSON:

dim number as integer = array(1,2,3,4)
dim thisJSON as new JSONItem
thisJSON.Value("Numbers") = numbers
dim s as string = thisJSON.ToString()

Thanks, I agree with Aaron I have to do it manually
Public Function ArrayIntToString(n() as integer) as string

dim s() as string
for each i as integer in n()
s.append str( i )
next
return join(s,";")

End Function

[quote=430298:@Enric Herrera]Thanks, I agree with Aaron I have to do it manually
Public Function ArrayIntToString(n() as integer) as string

dim s() as string
for each i as integer in n()
s.append str( i )
next
return join(s,";")

End Function[/quote]

CAUTION:
A For…Each loop does not guarantee that it will loop through the values in the array in index order.
So there is no guarantee that your integer array will have the same order as your string array.
Better use a “regular” For … Next loop if order matters.

[quote=430435:@Stefan Adelsberger]CAUTION:
A For…Each loop does not guarantee that it will loop through the values in the array in index order.
So there is no guarantee that your integer array will have the same order as your string array.
Better use a “regular” For … Next loop if order matters.[/quote]
Thank’s Stefan, I’m aware of this, but in that case, order is not relevant.

theres a post here somewhere where joe once said that the current ordering is first to last and the likelihood of that changing is very low low

Norman, you’re right but is good to be aware of this because the documentation says:
“Do not make any assumptions of the traversal order as it is subject to change in the future.”

Yeah - But joe was the compiler engineer so him saying “unlikely to change” is about as close to “its not changing” as you’ll get

esp if you’re dealing with an array where a sensible iterator goes from 0 to ubound

with other data types (say a dictionary) if the underlying representation was to move to some kind of tree then its very plausible the ordering could change. or even moving it from a hashmap keyed one way to one keyed differently then the order could change.