Jump to content

StringSplit() by length?


Recommended Posts

I am trying to write a script that will split a string by its length into the usual array, to be specific a 8-character string. For example:

$test = "this is a test string.... blahhhhhhh..."
$split = TheFunctionIAmLookingFor($test)
In the example, $split[0] would be the number of split strings, as StringSplit normally does, and
  • $split[1] = "this is "
  • $split[2] = "a test s"
  • $split[3] = "tring..."
  • $split[4] = ". blahhh"
  • and $split[5] = "hhhh..."
How could I do this?

Sorry if this is confusing, my questions usually are,

-- SANDMAN!

[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Link to comment
Share on other sites

#include <array2.au3>
$res=_SplitByChar("1234567890",2)
_ArrayDisplay($res)

Func _SplitByChar($String, $Chrs)
    $len = StringLen($string)
    $Parts = Round($len/$chrs,0)+1
    $split1 = StringSplit($String, "")
    Local $Split2[$parts] = [""], $i = 0
    For $a = 1 to $Parts-1
        For $b = 1 to $chrs
            $Split2[$a] &= $Split1[$b+($i*$chrs)]
        Next
        $i +=1
    Next
    $Split2[0] = $Parts-1
    Return $Split2
EndFunc

Edited by Paulie
Link to comment
Share on other sites

<-- Bertha

<-- says,

<-- "Thank

<-- you!"

#include <array.au3>
$res=_SplitByChar("1234567890",2)
_ArrayDisplay($res)

Func _SplitByChar($String, $Chrs)
    $len = StringLen($string)
    $Parts = Round($len/$chrs,0)+1
    $split1 = StringSplit($String, "")
    Local $Split2[$parts] = [""], $i = 0
    For $a = 1 to $Parts-1
        For $b = 1 to $chrs
            $Split2[$a] &= $Split1[$b+($i*$chrs)]
        Next
        $i +=1
    Next
    Return $Split2
EndFunc
Thank you!! Edited by sandman

[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Link to comment
Share on other sites

Unfortunately, it only works if the string is divided evenly by the number of characters you want to divide by.

try:
$test = "this is a test string.... blahhhhhhh..."

Dim $split[2]
$i = 1
Do
    $split[$i] = StringLeft($test, 8)
    $test = StringTrimLeft($test, 8)
    $i = $i + 1
    ReDim $split[$i + 1]
Until StringLen($test) < 8
$split[$i] = $test
$split[0] = UBound($split) - 1

For $i = 0 To $split[0]
    ConsoleWrite("$split[" & $i & "] = " & _
            $split[$i] & "    # of char " & _
            StringLen($split[$i]) & @CR)
Next
You can make it into a function if you wish. :-)

Edit: added code to display array...

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

#include <Array.au3>

_ArrayDisplay(_StringChop("this is a test string.... blahhhhhhh...",8))

Func _StringChop($string, $size)
$count = Ceiling(StringLen($string)/$size)
Dim $array[$count+1], $start = 1
For $i = 1 To $count
    $array[$i] = StringMid($string, $start, $size)
    $start += $size
Next
$array[0] = $count
Return $array
EndFunc

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

Siao,

add

$array[0] = UBound($array) - 1

above

_ArrayDisplay($array)

to make the OP happy :-)

"...$split[0] would be the number of split strings..."

Seriously, nice solution - I started with StringMid and changed to StringLeft and StringTrimLeft for some silly reason. StringMid is probably the better method.

-MSP-

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Whoops, forgot about that...

done.

Oh sure - do it the easy way with your $count var from the ceiling math...

Man, this is not my night for reading or writing code :-(

Don't be a stranger...

-MSP-

[size="1"][font="Arial"].[u].[/u][/font][/size]

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