String Things - Stand-alone string manipulation functions

Post your working scripts, libraries and tools
Verdlin
Posts: 63
Joined: 04 Oct 2013, 08:55
GitHub: Aatoz

Re: String Things - Stand-alone string manipulation function

29 May 2014, 04:35

Thanks, Tidbit!

@guest3456: Yes, those first braces are unnecessary. It's there because I paste all of my examples from a scratchpad function. It's doesn't hurt to include them in posts, and it is less work for me to remove braces and indentation.

@AfterLemon: I considered that at first, too. The particular case where I find this function beneficial is outlined below. It saves a little bit of coding headache and also reduces potential for small errors.

Shown below, I use this type of on-the-fly debugging on a daily basis, and the concat function makes my life a little easier...

Code: [Select all] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

User avatar
tidbit
Posts: 1083
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation function

30 May 2014, 14:59

2.6 is out.
I've been slacking and not paying attention to what I've changed/added/removed.
All I can remember is I just added st_concat() and have an alternative (better, but harder to read) version of st_printArr().
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
sv270190
Posts: 43
Joined: 06 Feb 2014, 11:48
Facebook: svsammaponnu@gmail.com

Re: String Things - Stand-alone string manipulation function

15 Aug 2014, 08:56

dear sir

what are the functions in which i can use the entire file and how could i get the output file

for eg st_insertline st_deleteline how can i use the entire file

i.e i want the 3rd of input.txt to be inserted with "new 3rd line" so that the original 3rd line is now the fourth line.

kindly guide me what are all the other functions in which the operation is conducted in the entire file
S.V. SRINIVASAN
SRIVILLIPUTTUR
TAMIL NADU
User avatar
tidbit
Posts: 1083
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation function

15 Aug 2014, 11:09

kindly guide me

you will need these commands and steps:

1. Read the file into a variable: http://ahkscript.org/docs/commands/FileRead.htm

2. Use this function from String Things to insert stuff where you want: giraffe:=st_insertLine("Pandas cannot fly", Variable, 3). %giraffe% will contain the new text.

3. Delete the original file from step [1] using: http://ahkscript.org/docs/commands/FileDelete.htm

4. Re-create the original file with the new text stored in %giraffe% using: http://ahkscript.org/docs/commands/FileAppend.htm

Note: I color-coordinated important stuff for you to easily study/learn.
I would suggest testing on a backup version of your file(s).
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
sv270190
Posts: 43
Joined: 06 Feb 2014, 11:48
Facebook: svsammaponnu@gmail.com

Re: String Things - Stand-alone string manipulation function

17 Aug 2014, 00:08

thank you for your immediate response/guidance
may i conclude that the subject file be read to a variable and that we can use string function / operation to the entire file

i will test and revert to you.
S.V. SRINIVASAN
SRIVILLIPUTTUR
TAMIL NADU
User avatar
tidbit
Posts: 1083
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation function

17 Aug 2014, 10:14

Working directly with files is not easy in AHK. There is already a library for that, TF.ahk. But iirc, it needs to do the same steps as I mentioned above (read, delete, build, rewrite). And it's massive. And with TF() you can't simply copy/paste the functions you need into your code and ignore everything else. You'll need all 1300+ lines.

so with ST(), you can remove the 1300 lines and just add 4 lines to read, delete, build then rewrite. Only if you need them.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
Stevoisiak
Posts: 12
Joined: 24 Aug 2017, 14:19
GitHub: Stevoisiak
Contact:

Re: String Things - Stand-alone string manipulation functions

01 Sep 2017, 12:44

Any particular reason to host this on DropBox and not GitHub? (My workplace blocks access to DropBox due to security concerns)
User avatar
RUNIE
Posts: 279
Joined: 03 May 2014, 14:50
GitHub: Run1e

Re: String Things - Stand-alone string manipulation functions

01 Sep 2017, 17:38

Stevoisiak wrote:Any particular reason to host this on DropBox and not GitHub? (My workplace blocks access to DropBox due to security concerns)

tidbit really likes dropbox.
User avatar
tidbit
Posts: 1083
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation functions

01 Sep 2017, 20:26

dropbox is nice and easy. git is annoying. I'll put it on pastebin instead.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
Stevoisiak
Posts: 12
Joined: 24 Aug 2017, 14:19
GitHub: Stevoisiak
Contact:

Re: String Things - Stand-alone string manipulation functions

06 Sep 2017, 10:14

I may be mistaken, but I didn't see any way of handling 2D arrays with st_split, such as ones found in CSV files.

Based on code by Oleg, here's a function for splitting a delimited string to a 2D array.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Example usage

Code: [Select all] [Download] GeSHi © Codebox Plus

jobString = 
(
Unit,Dept_ID,Name
CORP,0368,Admin
CORP,3945,Programmer
SESHAN,4596,Software Engineer
)

jobArray2D = ad_split2D(jobString, ",")
User avatar
tidbit
Posts: 1083
Joined: 29 Sep 2013, 17:15
Location: USA

Re: String Things - Stand-alone string manipulation functions

06 Sep 2017, 10:42

st_split is useless now. ahk finally has it built-in, strsplit()
then you work on it (either my function or the returned var of strsplit()) the same way you use arrays. arr[blah] arr[1] arr[5] etc

But I guess you knew that since you used it :P
yours does save a step for multi-lined inputs, though. neat.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
joefiesta
Posts: 149
Joined: 24 Jan 2016, 13:54

Re: String Things - Stand-alone string manipulation functions

14 Jan 2018, 18:03

Great library. Thanks

Question about St_Pad:

I found StrPad() function in the old forum by Majkinetor. But, it doesn't work. The character padded is not the correct character. I have to admit that I like the fact that it does not use a loop. It was found here: https://autohotkey.com/board/topic/2418 ... ntry156593
here is Majkinetor's StrPad():

Code: [Select all] [Download] GeSHi © Codebox Plus

StrPad(str, padchar, padlen, left=1){
if (i := padlen-StrLen(str))
VarSetCapacity(w, i, asc(padchar)), NumPut(0, &w+i, "Char"), VarSetCapacity(w, -1)
return left ? w str : str w
}


Can anyone get this to work?
User avatar
jeeswg
Posts: 4600
Joined: 19 Dec 2016, 01:58
Location: UK

Re: String Things - Stand-alone string manipulation functions

14 Jan 2018, 23:08

Perhaps look at SLICE STRING / PAD STRING, here:
jeeswg's RegEx tutorial (RegExMatch, RegExReplace) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28031

- [EDIT:] I've looked at the function, and it pads only on one side of a string, not within a string.
- It was written for AutoHotkey Basic, and works in AutoHotkey ANSI, I've rewritten the function in 2 different ways, both work with both AutoHotkey ANSI and AutoHotkey Unicode:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


- [EDIT:] Note: it's possible to fill binary data with consecutive bytes with the value 1, reading the data as ANSI: individual bytes are seen as Chr(1), reading the data as UTF-16: pairs of bytes are seen as Chr(257) (i.e. 0x0101 (little endian) = 1+256 = 257).
- [EDIT:] In short, VarSetCapacity is used with FillByte, or StrReplace and Format, as workarounds to repeat a string, and to avoid using Loop. I have asked for a StrRept function to be added to AutoHotkey in my Wish List 2.0.
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=13&t=36789
iPhilip
Posts: 287
Joined: 02 Oct 2013, 12:21

Re: String Things - Stand-alone string manipulation functions

15 Jan 2018, 15:03

Hi tidbit,

I noticed a problem with the ST_Insert() function when pos < 0. See the example below, which includes a simplified version of the function that fixes the problem.

Code: [Select all] [Expand] [Download] (ST_Insert2.ahk)GeSHi © Codebox Plus

Cheers!
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
iPhilip
Posts: 287
Joined: 02 Oct 2013, 12:21

Re: String Things - Stand-alone string manipulation functions

16 Jan 2018, 12:29

Using jeeswg's clever approach, st_pad() can be reduced to a on-liner :)

Code: [Select all] [Download] (st_pad.ahk)GeSHi © Codebox Plus

st_pad(string, left="0", right="", LCount=1, RCount=1) {
return StrReplace(Format("{:" LCount "}", ""), " ", left) string StrReplace(Format("{:" RCount "}", ""), " ", right)
}
Cheers!
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
derz00
Posts: 491
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: String Things - Stand-alone string manipulation functions

16 Jan 2018, 12:54

Hi tidbit, I have looked over these functions occasionally, very intrigued. Well written and documented. I notice you use recursion, especially for the array to string functions. Nice. :salute:

I am curious why you have some parameters like indentLevel in st_printArr(array, depth=5, indentLevel="") that you say "DO NOT TOUCH." Is that to save space and lines? Rather than declaring the variable on its own line?

Like this:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

try it and see
...

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Dravenizer and 20 guests