Sign in to follow this  
Followers 0
Info

Double StringReplace?

10 posts in this topic

$s = "ab"
$s = StringReplace($s, "a", "b")
$s = StringReplace($s, "b", "a")
MsgBox(0, "", $s)

I want 'a' to turn into 'b' and 'b' to turn into 'a' so the result should be "ba".

How can I do that?

Share this post


Link to post
Share on other sites



#Include <String.au3>
$s = "ab"
$s=_StringReverse($s)
MsgBox(64,"",$s)


[size="5"] [/size]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The first thing I came up with. I must have arrays on my mind..

$abString = "ab"
$abArray = StringSplit($abString, "", 2)
ConsoleWrite("What was " & $abString & " is now " & $abArray[1] & $abArray[0] & @LF)
Edited by snowmaker

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

try like this

Global $s = "ab1cbba1abaab"
$s = _CharSwap($s,"a","b")
MsgBox(0,"",$s)


Func _CharSwap($string, $first, $second)
    Local $b, $1 = StringLen($first),$2 = StringLen($second),$3 = StringLen($string)
    If $first <> $second And $1 = 1 And $2 = 1 And $3 <> 0 Then
        For $x = 1 To StringLen($string)
            $str = StringMid($string, $x, 1)
            Select
                Case $str = $first
                    $b = $b&$second
                Case $str = $second
                    $b = $b&$first
                Case Else
                    $b = $b&$str
            EndSelect
        Next
        Return $b
    Else
        Select
            Case $first = $second
                ConsoleWrite(@ScriptFullPath&" : ==> Identical replaceing char used.:"&@CR&"_CharSwap($string, $first, $second)"&@CR&"_CharSwap($string, ^ ERROR ^ ERROR"&@CR)
            Case $1 <> 1
                ConsoleWrite(@ScriptFullPath&" : ==> Invalid char number used.:"&@CR&"_CharSwap($string, $first, $second)"&@CR&"_CharSwap($string, ^ERROR"&@CR)
            Case $2 <> 1
                ConsoleWrite(@ScriptFullPath&" : ==> Invalid char number used.:"&@CR&"_CharSwap($string, $first, $second)"&@CR&"_CharSwap($string, $first  ^ERROR"&@CR)
            Case $3 = 0
                ConsoleWrite(@ScriptFullPath&" : ==> Invalid string used.:"&@CR&"_CharSwap($string, $first, $second)"&@CR&"_CharSwap( ^ERROR"&@CR)
        EndSelect
        Exit
    EndIf
EndFunc

Edited:

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

This is why your code doesn't do as you expect i've added a msgbox and commented what happens to your $s

$s = "ab"
$s = StringReplace($s, "a", "b") ; changes 'ab' to 'bb'
MsgBox(0, "", $s)
$s = StringReplace($s, "b", "a") ; changes 'bb' to 'aa'
MsgBox(0, "", $s)

GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.

Share this post


Link to post
Share on other sites

bogQ's example is the best one. thanks

Share this post


Link to post
Share on other sites

Just because we can :mellow:

$s = "ab1cba1abab"
$sNew = _CharSwap($s, "a", "b")
MsgBox(0, "", "WAS: " & $s & @CRLF & @CRLF & "NOW: " & $sNew)


Func _CharSwap($string, $first, $second)
  $string = StringRegExpReplace($string, "(?:(" & $first & ")(" & $second & ")|(" & $second & ")(" & $first & "))", "\2\1\4\3")
  Return $string
EndFunc   ;==>_CharSwap

Share this post


Link to post
Share on other sites

@ResNullius

not working correctly on this string :lol: "ab1cbba1abaab"

I know that probably StringRegExp have power to do that, and thats why i did not use it :mellow: becose i still did not learn how to use that command :(


TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

We're still unsure of what the OP was asking for. It could be either:

-) reversing a string

-) exchanging the positions of characters in a 2-character string

-) generic exchange of a given codepoint with another one.

Solutions already given covers the three aspects, more or less.

In the most general case, either you walk the string character after character and make replacements on the fly or use a temporary placeholder codepoint in a two three pass approach.

Since there is already one generic solution covering the former, I'll give one for the later.

I assume there is at least one codepoint in the 0x0000 ... 0xFFFF range that can be used as placeholder. Remember that AutoIt strings currently adhere to UCS-2 rather that UTF-16, so surrogate codepoints would be valid placeholders. But since it's likely that future revisions of AutoIt will comply with UTF-16LE Unicode, we can't use them reliably. Instead, we can use almost any codepoint in the "private use" range, assuming the one chosen is not present in the source string. Such "private use" range is 0xE000 to 0xF8FF, which leaves enough candidates for what we need (only one codepoint!).

Local $input1 = 'ahb hdzjhoigsohabebafrnzjkl    b--a'
ConsoleWrite($input1 & @LF & _SwapSubStrings($input1, 'a', 'b') & @LF)

Local $input2 = 'ahb hdzjhoigsohahbebafrnzjkl   b--a'
ConsoleWrite($input1 & @LF & _SwapSubStrings($input2, 'ahb', 'zj') & @LF)       ; works for substrings as well

Func _SwapSubStrings($s, $s1, $s2)
    Local Const $placeholder = ChrW(0xF123)         ; rare enough codepoint in the "private use" 0xE000..0xF8FF range
                                                    ; might be chosen by scanning the input string until one unused is found
    $s = StringRegExpReplace($s, $s1, $placeholder)
    $s = StringRegExpReplace($s, $s2, $s1)
    Return(StringRegExpReplace($s, $placeholder, $s2))
EndFunc

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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  
Followers 0