Jump to content

Convert string to array


AutID
 Share

Recommended Posts

Since you seem to know the size of the array beforehand:

#include <Array.au3>

Global $string[3] = ['']

For $i = 1 to 3
  $string[$i - 1] = "test" & $i
Next

_ArrayDisplay($string)

Not too much different.  Just cleaned up some.

Link to comment
Share on other sites

Since you seem to know the size of the array beforehand:

#include <Array.au3>

Global $string[3] = ['']

For $i = 1 to 3
  $string[$i - 1] = "test" & $i
Next

_ArrayDisplay($string)

Not too much different.  Just cleaned up some.

I dont know the size of the array. Depends on the runs.

I want to turn a string to array. Not declare an array and then add to it.

Edited by AutID
Link to comment
Share on other sites

Use StringSplit.

;

#include <Array.au3>

Local $sString = "", $iMax = 10, $sDelim = ","

For $i = 1 To $iMax
    $sString &= "test" & $i & $sDelim
Next
$sString = StringTrimRight($sString, 1)

Local $aArray = StringSplit($sString, $sDelim, 2)
_ArrayDisplay($aArray)
Link to comment
Share on other sites

 

Use StringSplit.

;

#include <Array.au3>

Local $sString = "", $iMax = 10, $sDelim = ","

For $i = 1 To $iMax
    $sString &= "test" & $i & $sDelim
Next
$sString = StringTrimRight($sString, 1)

Local $aArray = StringSplit($sString, $sDelim, 2)
_ArrayDisplay($aArray)

I thought about that solution as well but then i will have to add a seperator everytime. Not that it is a big deal but i was just wondering if there are other way than these two here.

Edit: please don't feel offended. Thank you and everyone for helping me. I just want to see other solution as well.

Edited by AutID
Link to comment
Share on other sites

If you know a repeating pattern will occur, then a third way would be to use regular expressions. In this case you are using a test string which is not what you will use in every case. It is not possible to create a general solution. A regular expression can often be written in different ways.

;

#include <Array.au3>

Local $sString = "test1test2test3"

Local $aArray = StringRegExp($sString, "(?i)([a-z]+[0-9]+)", 3)
_ArrayDisplay($aArray)
Link to comment
Share on other sites

Nice one, I had gotten this and now I see why it doesn't work:

#include <Array.au3>

Global Const $string = "Test1Test2Test3"

Global Const $array = StringRegExp($string, "(?i)(\w+\d+)", $STR_REGEXPARRAYMATCH)

_ArrayDisplay($array)
Link to comment
Share on other sites

I can't use @replaces because string will not be the same. It is going to be random.

 

You have to have some defining criteria to know where to split the string. I quite often split a string into equal length segments using a function I wrote: _StringEqualSplit(). The criteria here is the length of each split.

Edited by czardas
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...