Jump to content

Split String off into subsections/ groups


Recommended Posts

Example: 234782349 firstname lastname email

I want to take the above string and spit it off into different sections. A section for the number, firstname, lastname, email. How would I go about doing that?

#Include <Array.au3>
$sString =  "234782349 firstname lastname email"
$OneSpace = " " ;If more than 1 space apart, add more spaces here to match.
See helpfile.
$array = StringSplit($sString, $OneSpace)
_ArrayDisplay($array)
Link to comment
Share on other sites

Then you can use StringRegExpReplace and something like:

#Include <Array.au3>
$sString =  "234782349      firstname              lastname      email" & @CRLF
$sString &=  "234782349 firstname          lastname    email"

$sString = StringRegExpReplace($sString, '\h+', ' ')
$aLines = StringSplit($sString, @CRLF, 1)

For $i = 1 To $aLines[0]
    $array = StringSplit($aLines[$i], ' ')
    _ArrayDisplay($array)
Next
Link to comment
Share on other sites

Then you can use StringRegExpReplace and something like:

#Include <Array.au3>
$sString =  "234782349      firstname              lastname      email" & @CRLF
$sString &=  "234782349 firstname          lastname    email"

$sString = StringRegExpReplace($sString, '\h+', ' ')
$aLines = StringSplit($sString, @CRLF, 1)

For $i = 1 To $aLines[0]
    $array = StringSplit($aLines[$i], ' ')
    _ArrayDisplay($array)
Next
Thanks a ton!! You were the biggest help. I have one more question for you. Is it possible to change the name of the row from [0] to anything else?
Link to comment
Share on other sites

Mind to explain some more? Do you mean, changing $aLines[0] to something else? If so, you'll need first to check if the new something else is not exceeding the last array's subscript.

From This

[0]|General information

[1]|234890934867

[2]|laskdfjnkjn1

[3]|Doe

[4]|John

[5]|jdoe@somethinganother.com

To This

|General information

# |234890934867

ID |laskdfjnkjn1

Lastname|Doe

Firstname|John

Email |jdoe@somethinganother.com

If this is not possible, I could just throw it through a 2D array

Link to comment
Share on other sites

Use Enum. You can give a subscript index element a meaningful name so it's index is self-explaining:

Enum $GenInfo, $Num, $ID, $Lastname, $Firstname, $Email, $UpperBound

; ...
For $i = 1 To $aLines[0]
    $array = StringSplit($aLines[$i], ' ')
    If UBound($array) = $UpperBound Then
         ConsoleWrite('General Information: ' & $array[$GenInfo] & @LF)
         ConsoleWrite('Member number: ' & $array[$Num] & @LF)
         ;....
    EndIf
Next

Or as you mentioned, it's possible to implement it using a 2D array but the enumeration method is preferred in my opinion.

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...