Jump to content

Replace string in array trouble


Recommended Posts

Hello,

I'm using a function I found in the forums to split a string into an array where each index contains 1 character:

Local $res = _SplitByChar($userInput, 1)

Func _SplitByChar($userInput, $nChars)

Local $Array = ''

For $iCC = 1 To StringLen($userInput) Step $nChars

$Array &= StringMid($userInput, $iCC, $nChars) & Chr(1)

Next

If $Array Then Return StringSplit(StringTrimRight($Array, 1), Chr(1))

Return SetError(1, 0, '')

EndFunc

Then based on the index and the character performing specific functions:

Do

If $res[$Char] = "." or $res[$Char] = ";" or $res[$Char] = "," Or $res[$Char] = ":" Then

function()

and trying to replace those characters using stringreplace:

StringReplace(StringReplace(StringReplace(StringReplace($res[$Char], ":", "-"), ";", "-"), ".", "-"), ",", "-")

So that next loop through the array I should have a different command based on the new character.

Until $b = $res[0] * 2

I'm not sure, but I think the problem is every time it calls $res[$char] it runs the _SplitByChar function and undoing my string replacements.

I'm pretty new at this and it's probably something easy, but any help would be appreciated.

Thanks

Link to comment
Share on other sites

To get one character in each element do this.

#include <Array.au3> ; For _arrayDisplay
Local $string = "12345abcde"
Local $aArray = StringSplit($string, "", 2)
_arrayDisplay($aArray)

I didn't understand the rest of the question. Perhaps you could explain it better.

Edited by czardas
Link to comment
Share on other sites

#include <Array.au3> ; For _arrayDisplay
Local $string = "12345:ab:cde"
Local $aArray = StringSplit($string, "", 2)
_arrayDisplay($aArray, "Before")

Local $searchChar = ":", $replaceChar = "-"
For $i = 0 To Ubound($aArray) -1
    If $aArray[$i] = $searchChar Then $aArray[$i] = $replaceChar
Next
_arrayDisplay($aArray, "After")

In this case you could also replace the characters in the string before creating the array.

#include <Array.au3> ; For _arrayDisplay
Local $string = "12345:ab:cde"
$string = StringReplace($string, ":", "-")
Local $aArray = StringSplit($string, "", 2)
_arrayDisplay($aArray, "Alternative Method")
Edited by czardas
Link to comment
Share on other sites

I actually need the characters in the array. The character along with the position in the string tell tell the program what to do. The character position in the string tells the mouse where to move to, and the character tells it which function to carry out. So I want it to carry out one function, change the character, and perform a second different function on the next loop.

Your first example worked, but I have to replace them all at once at the end of the first loop through the string. I have another question about it though.

About this one:

#include <Array.au3> ; For _arrayDisplay

Local $string = "12345:ab:cde"

Local $aArray = StringSplit($string, "", 2)

_arrayDisplay($aArray, "Before")

Local $searchChar = ":", $replaceChar = "-"

For $i = 0 To Ubound($aArray) -1

If $aArray[$i] = $searchChar Then $aArray[$i] = $replaceChar

Next

_arrayDisplay($aArray, "After")

Can I use multiple characters, instead of just colon to dash, I would like to change colon, semicolon, comma, and period to a dash.

Edited by BillFrosby
Link to comment
Share on other sites

Not much time today - in between music lessons. Try this.

#include <Array.au3> ; For _arrayDisplay
Local $string = "12,.3;45:ab:cde"
Local $aArray = StringSplit($string, "", 2)
_arrayDisplay($aArray, "Before")

For $i = 0 To Ubound($aArray) -1
    $aArray[$i] = StringRegExpReplace($aArray[$i], "[,:;.]", "-")
Next
_arrayDisplay($aArray, "After")

Also try putting the code you post inside AutoIt code tags like this:

[ autoit ] code [ /autoit ]

only without the spaces.

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