Jump to content

StringSplit, only with an odd split


 Share

Recommended Posts

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

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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