Jump to content
Sign in to follow this  
Mat

StringSplit, only with an odd split

Recommended Posts

Mat

basically, I have a string

$String = "111111111100000000000111000000000000000"

now I need to get this:

$Split = Stringsplit ($String)

$Split[0] = 4
$Split[1] = "1111111111"
$Spit[2] = "00000000000"
$Split[3] = "111"
$Split[4] = "000000000000000"

I was trying this:

$String = "111111111100000000000111000000000000000"
$Split = Stringsplit ($String, "")
$i = 1
Do
   $m = 1
   $Done[$m] = $Split[$i]
   Do
      $n = 1
      If $Split[$i + $n] = $Split[$i] Then
         $Done[$m] &= $Split[$i + $n]
      Else
         ExitLoop
      EndIf
      $m += 1
   Until $n + $i = $Split[0]
Until $i = $Split[0]

MsgBox (48, "test", $Done)

but no result.

if it helps I can get away with only 2 different string values, but ideally it should cater for more....

I hope that code is useful, it's incredbly confusing, but do you see what I'm trying to do? I was using for loops, but do's became easier when I started mixing up values....

Share this post


Link to post
Share on other sites
JRowe

This is more of a stringregexp function than a stringsplit function (although you could probably do it either way.)

Something like this might work, returning an array of matches for multiple groupings of any character.

StringRegExp("111111111100000000000111000000000000000", "(.+)" 2)

Share this post


Link to post
Share on other sites
Mat

ta muchly, never used that before, will try it out!

won't be here for a week, so your better off pming me with any further answers.

Thanks

MDiesel

Share this post


Link to post
Share on other sites
Stilgar

Hello,

I think you searching something like this:

$s = "111111111100000000000111000000000000000"
$a = StringRegExp($s,"[1]+|[0]+",3)
_ArrayDisplay($a)

Share this post


Link to post
Share on other sites
BrettF

Does this work?

#Include <Array.au3>
$string = "111111111100000000000111000000000000000"

$aStrings = _StringSplitByChange($string)
_ArrayDisplay ($aStrings)

Func _StringSplitByChange($sString)
    Local $iCount = 1
    Local $asplit = StringSplit ($sString, "")
    Local $aRet[$asplit[0]]
    $aRet[1] = $asplit[1]
    For $i = 2 to $asplit[0]
        If $asplit[$i-1] <> $asplit[$i] Then
            $iCount += 1
            $aRet[$iCount] &= $asplit[$i]
        Else
            $aRet[$iCount] &= $asplit[$i]
        EndIf
    Next
    ReDim $aRet[$iCount+1]
    $aRet[0] = $iCount
    Return $aRet
EndFunc

Share this post


Link to post
Share on other sites
Malkey

basically, I have a string

$String = "111111111100000000000111000000000000000"

now I need to get this:

$Split = Stringsplit ($String)

$Split[0] = 4
$Split[1] = "1111111111"
$Spit[2] = "00000000000"
$Split[3] = "111"
$Split[4] = "000000000000000"

I was trying this:

$String = "111111111100000000000111000000000000000"
$Split = Stringsplit ($String, "")
$i = 1
Do
   $m = 1
   $Done[$m] = $Split[$i]
   Do
      $n = 1
      If $Split[$i + $n] = $Split[$i] Then
         $Done[$m] &= $Split[$i + $n]
      Else
         ExitLoop
      EndIf
      $m += 1
   Until $n + $i = $Split[0]
Until $i = $Split[0]

MsgBox (48, "test", $Done)

but no result.

if it helps I can get away with only 2 different string values, but ideally it should cater for more....

I hope that code is useful, it's incredbly confusing, but do you see what I'm trying to do? I was using for loops, but do's became easier when I started mixing up values....

This example groups any consecutive repeating characters and, individual non - repeating characters if they exist in the test string, into an array.

If case sensitivity is required, remove "(?i)" from the beginning of the regular expression search pattern.

#include <Array.au3>

$sStr = "111111111100000000000111000000000000000CCcC11abBc d%%%!"

; Groups repeating characters & stand alone characters into an array.
$aArray = StringSplit(StringTrimRight(StringRegExpReplace($sStr, "(?i)(.)(\1*)", "\1\2|"), 1), "|")

_ArrayDisplay($aArray)

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.