Jump to content

StringSplit


Recommended Posts

Local $k = 0 , $v = 1, $d = "|||", $us , $emotion, $what
Local $info = "We|||Love|||autoit|||"
$pairs = StringSplit($info, $d ,2)

Alright my whole thread is changing, because no matter how long you look at something until you post it you will never get it. At this point I have what I wanted working to an extent. But String Split leaving weird spaces in the in the array. Any way to prevent this ? 

[0]|data=foo
[1]|
[2]|
[3]|data2=bar
[4]|
[5]|
[6]|data3=hippie
[7]|
[8]|
[9]|
Edited by USMCGalloway
Link to comment
Share on other sites

you used:

$pairs = StringSplit($info, $d ,2)

but you want to split the string with string delimiter, and if you still want to disable return of item count as first element you would have to use this:

$pairs = StringSplit($info, $d ,2+1)

And finally, you would have to use this:

for $i = 0 To UBound($pairs)-1 ;instead of for $i = 0 To UBound($pairs) step 1

because of the zero-index counting.

 

 

EDIT: You have changed your first post, but did you even look at the explanation I gave you?

Edited by dragan
Link to comment
Share on other sites

USMCGalloway,

It is in your benefit to leave posts in tact as originally written and amend info using notation such as "edit: - " followed by change/explanation.

This avoids confusion for anyone following the thread who may be able to help.  You may also refer back to this thread in the future and forget what you changed.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Local $k = 0 , $v = 1, $d = "|||", $us , $emotion, $what
Local $info = "We|||Love|||autoit|||"
$pairs = StringSplit($info, $d ,2)
Alright my whole thread is changing, because no matter how long you look at something until you post it you will never get it. At this point I have what I wanted working to an extent. But String Split leaving weird spaces in the in the array. Any way to prevent this ? 

 

[0]|data=foo

[1]|

[2]|

[3]|data2=bar

[4]|

[5]|

[6]|data3=hippie

[7]|

[8]|

[9]|

 

Run this example and see comments in this example.

#include <Array.au3> ; For _ArrayDisplay purposes only

Local $d = "|||"
Local $info = "We|||Love|||autoit|||"

If StringRight($info, StringLen($d)) = $d Then $info = StringTrimRight($info, StringLen($d)) ; Remove trailing $d if present.
If StringLeft($info, StringLen($d)) = $d Then $info = StringTrimLeft($info, StringLen($d)) ; Remove leading $d if present.

; See StringSplit function parameter "flag"
; flag = 1, entire delimiter string is needed to mark the split
; flag = 2, disable the return the count in the first element - effectively makes the array 0-based (must use UBound() to get the size in this case).
; (flag 1 + flag 2 = flag 3)
Local $aPairs = StringSplit($info, $d, 3) ; flag 3
_ArrayDisplay($aPairs)
Link to comment
Share on other sites

If $pairs[$i] = "" Then ContinueLoop
In your for loop at the top of it.

Local $d = "|||"
Local $info = "We|||Love|||autoit|||"
$pairs = StringSplit($info, $d , 2)
For $i = 0 To UBound($pairs) - 1
    If $pairs[$i] = "" Then ContinueLoop
    ConsoleWrite($pairs[$i] & @CRLF)
Next

 

If you put a hole in a bucket only so that you can fix it , then here is another fix.

Local $d = "|||"
Local $info = "We|||Love|||autoit|||"
$pairs = StringSplit($info, $d, 2)
For $i = 0 To UBound($pairs) - 2 Step Stringlen($d)
    ConsoleWrite($pairs[$i] & @CRLF)
Next
Edit: Changed "Step 3"

To

"Step Stringlen($d)"

alluding to the fact that the flag 2 parameter of the StringSplit function is the cause of all the extra blank elements added to the resultant array.

Edited by Malkey
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...