Android Question String to Int Array

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I am reading a data file that has a string of data (numbers separated by commas) like this 101,214,345,423,5,6,7

I have been parsing this string to fill an array of Ints and I was wondering if there was a way to do this:

B4X:
     Dim StringArray As String = "1,2,3,4,5,6,7,8,9"  '  Read the string of data into a string
     Dim IntArray()  As Int

	
	IntArray = Array As Int(StringArray)         '  IS THERE ANY Way to tell the complier I want what is in the string not the string?

Normally I would manually init an array with something like this IntArray = Array as Int(1, 2, 3, 4, 5)

Which is pretty much the same as what is inside my string.

This would really speed the code up if I did not have to parse the individual entries.

BobVal
 

Mahares

Expert
Licensed User
Longtime User
You can do this:
B4X:
Dim StringArray As String = "1,2,3,4,5,6,7,8,9" 
Dim strArray()  As String =Regex.Split(",",StringArray)
Log(strArray(6))  'displays 7
Log (strArray(7)/strArray(1)) 'displays 4

or this:
B4X:
Dim StringArray As String = "1,2,3,4,5,6,7,8,9" 
Dim strArray()  As String =Regex.Split(",",StringArray)
Dim intArray(strArray.Length) As Int
For i = 0 To strArray.Length-1
  intArray(i)=strArray(i)
Next
Log(intArray(6))  'displays 7
Log (intArray(7)/strArray(1)) 'displays 4
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Not sure if I found a bug in Regex.Split the prototyping implies that the first parameter can be ANY type of Pattern.

The string that is coming into to my program has periods (decimals) not commas - I have been replacing all "." with ","
B4X:
PassString.Replace(".", ",")

I figured I would do this with Regex (trying to save the replacement)
B4X:
StringArray = Regex.Split(".", PassString)

But all I get is an Empty array.

If I do the following
B4X:
NewString = PassString.Replace(".", ",")
StringArray = Regex.Split(",", NewString)

Everything works. So Regex.Split will not take a period as a Pattern?

Otherwise Regex.Split works fine. Thanks for the idea.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…