Info Posted February 21, 2010 Posted February 21, 2010 $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?
Fire Posted February 21, 2010 Posted February 21, 2010 #Include <String.au3> $s = "ab" $s=_StringReverse($s) MsgBox(64,"",$s) [size="5"] [/size]
somdcomputerguy Posted February 21, 2010 Posted February 21, 2010 (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 February 21, 2010 by snowmaker - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change.
bogQ Posted February 21, 2010 Posted February 21, 2010 (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 February 22, 2010 by bogQ TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) 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.
Yoriz Posted February 21, 2010 Posted February 21, 2010 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.
Mison Posted February 22, 2010 Posted February 22, 2010 Do triple string replacement... $s = "ab" $s = StringReplace($s, "a", "xx101xx") ; replace with something unique $s = StringReplace($s, "b", "a") $s = StringReplace($s, "xx101xx", "b") MsgBox(0, "", $s) Hi ;)
ResNullius Posted February 22, 2010 Posted February 22, 2010 Just because we can $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
bogQ Posted February 22, 2010 Posted February 22, 2010 @ResNullius not working correctly on this string "ab1cbba1abaab" I know that probably StringRegExp have power to do that, and thats why i did not use it becose i still did not learn how to use that command TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) 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.
jchd Posted February 22, 2010 Posted February 22, 2010 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 hereRegExp tutorial: enough to get startedPCRE 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now