Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

StringMod() String Manipulation - Enhanced by PhiLho / Titan


  • Please log in to reply
77 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

That's because on a long string, the StringSplit will have to find and allocate all the variables, even if only the first is requested. While the Loop Parse stops as soon as it reached its goal, and drops intermediary results.


I agree .. :) This was the exact thing I wanted to tell :shock: . I am too tired working with Cheetah2.dll .. :D

Goyyah, why do you hate so much space? I like to put a space after commas and around = or :=, I find it easier on the eye, code breathes... :-)


Yes it looks very bad in BBCode .. however "not-so-bad" in my editor .. I always use space after a comma.. I do not know why I changed it! :shock: . I should return back to my habit.

Regards, :)
kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Conceptually, I prefer Goyyah & toralf's way than your, Titan.

I thought it was all about code size. Personally I would use toralf's method because it's the fastest.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
I am using toralf's code already ..

Conceptually, I prefer Goyyah & toralf's way than your, Titan.


but, PhiLho must have meant Loop,Parse Vs StringSplit method.

Regards, :)
kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Following a recent discussion I remembered this thread and how regular expressions can be used to simplify most things. Here's a new version with comments:
StringMod(string, method, param1 = "", param2 = "") {

	If method = Only ; remove all characters not in param1 from string:

		new := RegExReplace(string, "i)[^" . param1 . "]")

	Else If method = Omit ; remove characters in param1 from string:

		new := RegExReplace(string, "i)[" . param1 . "]")

	Else If method = Flip ; flip string backwards:

		Loop, % length := StrLen(string) ; loop for the length of the string

			new .= SubStr(string, length - A_Index, 1) ; get character from the end going backwards

	Else If method = Replace ; replace param1 with param2

		new := RegExReplace(string, "i)" . param1, param2)

	Else If RegExMatch(method, "Rot(?:ate)?(\d+)", rotN) ; for Rot13 and Rot47:

	{

		Loop, Parse, string ; for each character in the string:

			new .= Chr((chr := Asc(A_LoopField)) + rotN1) - 94 * (chr > 126)) ; apply rotation transformation

				; this increases the ASCII value of the character by the specified rotation amount

				; if the value exceeds 126 (highest character) subtract by 94 (character range)

	}

	Else If method = Scramble ; rearrange characters in random order

	{

		new := RegExReplace(string, "(.)", "$1÷") ; delimit each character with '÷'

		; this high-ASCII character is unlikely to exist in string, if it does it will be removed

		Sort, new, Random Z D÷ ; randomly sort

		new := RegExReplace(new, "÷") ; remove delimiter

	}

	Else If method = Bloat ; pad each character with param1 and param2:

		new := RegExReplace(string, "(.)", param1 . "$1" . param2)

	Else If method = Pattern ; return the number of times each character appears in the string:

	{

		unique := RegExReplace(string, "(.)", "$1÷") ; delimit each character with '÷' (like in 'Scramble') 

		Sort, unique, U Z D÷ ; remove duplicates

		unique := RegExReplace(unique, "÷") ; remove delimiter

		Loop, Parse, unique ; for each unique character in the string:

		{

			StringReplace, string, string, %A_LoopField%, , UseErrorLevel ; get the number of replacements

			new .= A_LoopField . ErrorLevel ; store this alongside the character itself

		}

	}

	Return, new ; return the modified string

}
Untested but I'm sure it works.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
As long as the parameters are correctly escaped, if needed...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

As long as the parameters are correctly escaped, if needed...

Sorry did I miss something?

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Examples of not working parameters:
StringMod(str, "Omit", ".-,;")
StringMod(str, "Replace", "*", "o")
etc.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Nice work Titan :D.
I am yet to test it, but I know it will work fine.
FYI: I have linked that post in the title post.

Regards, :)
kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

As long as the parameters are correctly escaped, if needed... StringMod(str, "Omit", ".-,;")

Oh yeah, param1 := RegExReplace(param1, "([\[\\\^\$\.\|\?\*\+\(\)])", "\$1") should be enough right?

I have linked that post in the title post.

The link to StringMod.ahk on my site still works btw.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

The link to StringMod.ahk on my site still works btw.


Oh! So the old version is not available! :(
kWo4Lk1.png

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Titan, :)

Thanks for the RegEx version. Iam using Scramble in one of my scripts.

new := RegExReplace(string, "(.)", "$1ö") ; delimit each character with 'ö'
		; this high-ASCII character is unlikely to exist in string, if it does it will be removed
		Sort, new, Random Z Dö ; randomly sort
		new := RegExReplace(new, "ö") ; remove delimiter

Regards, :)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
It looks slightly safer to use a non-printable ANSI character for delimiter:
string = qwertyu iop[

new := RegExReplace(string, "(.)", "$1" Chr(1))

Sort new, % "Random Z D" . Chr(1)

StringReplace new, new, % Chr(1),,All

MsgBox %new%


silkcom
  • Members
  • 162 posts
  • Last active: Jul 31 2015 10:57 PM
  • Joined: 23 Jan 2008
Sorry to awaken this old thread, SKAN do you still have your anagrammer.zip file? That's exactly what i'm trying to make :).

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Sorry to awaken this old thread, SKAN do you still have your anagrammer.zip file?


You may download a copy of words.db ( 2.47 MiB ) which is the datafile for the anagram script posted in this topic.

silkcom
  • Members
  • 162 posts
  • Last active: Jul 31 2015 10:57 PM
  • Joined: 23 Jan 2008
Hmm, I don't see the anagram script, only the code for the function