slice string, pad characters

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 4952
Joined: 19 Dec 2016, 01:58
Location: UK

slice string, pad characters

10 Apr 2018, 21:55

- I've been preparing some function libraries to share. I thought I'd share this one particular function now.
- Btw, do make any suggestions re. whether it should be called StrPad or something else. Essentially the idea is to insert a string every n characters.

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


[EDIT:] Renamed from 'StrPad' to 'StrHatch'. Another potential name is 'StrDelimit'.
[EDIT:] See a more appropriately named 'StrPad' function, lower down:
slice string, pad characters - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=47079&p=237105#p237105
Last edited by jeeswg on 06 Sep 2018, 18:15, edited 3 times in total.
User avatar
kczx3
Posts: 611
Joined: 06 Oct 2015, 21:39

Re: slice string, pad characters

11 Apr 2018, 20:10

StrPad is a horrible name. As is StrSlice. It does neither of those things.

Maybe StrDelimit?
User avatar
jeeswg
Posts: 4952
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

11 Apr 2018, 22:34

- Keep trying. Thanks.
- I don't like the word 'slice', but it does seen pretty apt.
- [EDIT:] Re. 'delimit'. Have you ever seen 'delimit' used in a function name?
- In Excel, text to columns (delimited), splits text, this adds characters (unless you use the object option).
- Some other ideas, but I need more ideas.
StrIntermix
StrInsert
StrIntersperse
StrInterlace
StrMingle
Stringle
Last edited by jeeswg on 13 Apr 2018, 15:57, edited 1 time in total.
User avatar
derz00
Posts: 495
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: slice string, pad characters

12 Apr 2018, 19:34

yes this is delimiting. Excel can split data by delimiters. Not sure where you researched your terms?

padding is adding characters to meet a certain length. such as padding 12 with 0's to 5 digits would be 00012

Slicing is slicing--which you do in AHK with SubStr()
try it and see
...
User avatar
jeeswg
Posts: 4952
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

12 Apr 2018, 20:05

- Pad. If you look at the dictionary definition for the verb 'pad', it commonly means to fill/intersperse. On searching the Internet for 'pad' I noticed that it was often used in IT contexts to refer to adding characters to the beginning/end of a string. Hence why I asked for other ideas.
- Slice. I mentioned the word 'slice' because IIRC someone had written a similar function and used the word 'slice'. I've seen that JavaScript has 'slice', and that it's similar to AHK's SubStr, but do you have concrete examples form other programming languages to back up your claim?
- Crucially, has anyone seen an existing function that works like this one, to add a string every n characters?
User avatar
kczx3
Posts: 611
Joined: 06 Oct 2015, 21:39

Re: slice string, pad characters

12 Apr 2018, 20:11

I doubt anyone has seen such a function before because no one has had the need for such functionality. And if they did, they didn’t feel like posting whatever solution they came up with.
User avatar
jeeswg
Posts: 4952
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

12 Apr 2018, 20:16

Do a Google search for: add string every n characters.
User avatar
derz00
Posts: 495
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: slice string, pad characters

13 Apr 2018, 07:41

Alright, by elimination, pad isn't ideal.

About slice:
jeeswg wrote:I've seen that JavaScript has 'slice', and that it's similar to AHK's SubStr, but do you have concrete examples form other programming languages to back up your claim?
You're right; SubStr is slicing.
What claim did I make? Pardon my misunderstanding.

Here's how you slice in python. The for-loop is just to show that strings are also iterable, so you can index a string just like an array (called a list in Python)

Code: [Select all] [Expand] [Download] (Untitled.py)GeSHi © Codebox Plus



And about delimit: I don't know what I'd use this function for except to delimit a string. It isn't something you usually do, hence this difficulty in naming it--there isn't a "real name" for it in the programming world.

I guess my point is just that this isn't padding or slicing, but that's as far as I can get with helping you with ideas. :)
try it and see
...
User avatar
nnnik
Posts: 3184
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: slice string, pad characters

13 Apr 2018, 08:05

intersperse seems to be the word we are looking for
Recommends AHK Studio
User avatar
jeeswg
Posts: 4952
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

13 Apr 2018, 12:12

- @derz00. Uses.
- This kind of thing is useful for grouping things into 3 (numbers), or 2/4/8/16 (numbers/hex/lists of Unicode characters).
- It's also useful for splitting up hex for use with WinMerge, when comparing two files.
- And it's good for taking binary data stored as hex, and parsing a list of 4-byte (8-hex character) pixel ARGB/BGRA values.
- It's also useful for 'padding' a string with asterisks (e.g. inserting a string every character: .*) when doing RegEx searches.
- Also, efficiently splitting a string, starting from the right can be a bit fiddly.

- Re 'slice':
[CharBloat/StringSlice, similar to my function]
StringMod() String Manipulation - Enhanced by PhiLho / Titan - Page 3 - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/7706-stringmod-string-manipulation-enhanced-by-philho-titan/page-3
[a different StringSlice, that works more like derz00 described]
Crazy Scripting : StringSlice() for FileToHex - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/58853-crazy-scripting-stringslice-for-filetohex/

- You'd said that 'Slicing is slicing', which implies that everyone in the IT world is in agreement. I wasn't particularly familiar with it, I did a search and noticed a JavaScript reference.
- Like with all meta discussions, where I ask re. any precedents, it would help if you could mention explicitly any languages you know that use it. E.g. is the concept used in old/new languages, in well-known/little-known languages, in something that sees more widespread use like Excel VBA, batch files, html, or LaTeX.

- I'm not sure if 'StrDelimit' would have a clear meaning, or even be suggestive of anything. A good word would describe what it does.

- @nnnik: Hmm, StrIntersperse isn't bad, a bit long though.
- Ideally the word would be short, not start with 'in', because the 'in' words are a bit bland, and not start with 's'. Also, I didn't particularly like the word 'slice'.
- Some other ideas, although not necessarily great are:
StrPepper
StrSow (sow seeds at regular intervals)
- I tried looking up various words on Thesaurus.com:
insert, mix, suffuse, pepper, intersperse.
Helgef
Posts: 3147
Joined: 17 Jul 2016, 01:02
Contact:

Re: slice string, pad characters

13 Apr 2018, 15:36

To insert at every n:th char, it is a simple regex. Maybe someone can do the "r" option with regex too? Putting aside that I might have made a mistake, the o option is not equivalent since it requires a delimiter,

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

; v2
stringle(byref str, n, c := ",", opt := ""){
return ( ( instr(opt, "r") ? (o:=mod(strlen(str),n), s:=substr(str,1,o), str:=substr(str,o+1)): "")
, str:=(s!=""?s c:"") regexreplace(str, "(.{" n "}(?!$))", "$0" c), instr(opt,"o") ? strsplit(str, c) : str )
}
; Example
msgbox stringle("abcdefghij", 3, "`n", "r")

Consider it a showcase of my dynamic style ;). Also note the choice of function name, my favourite suggestion :thumbup: .

Cheers, and thanks for sharing :) .
User avatar
jeeswg
Posts: 4952
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

13 Apr 2018, 15:47

- Nicely done Helgef.
- Btw if the pad text contains $, there might be an issue when RegEx is used.
- One of the reasons I added in the object functionality, is for situations where the text might already include the pad text. So if you then split by the pad text, it might not work correctly.
- A StrRept function might be useful for handling splits from the right.
User avatar
jeeswg
Posts: 4952
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

12 May 2018, 16:14

- Some more ideas, using physical imagery as a guide:
StrHatch (like hatch marks on a ruler, a mark is made at regular intervals)
StrButton (like buttons on a shirt)
StrSpread

- It seems that 'intersperse', and 'intercalate', are used in Haskell and have a different meaning.
- I thought that 'intersperse' was a good candidate, despite being quite long. (Although since the function would be used rarely, length wouldn't be too much of a problem.)
- Links:
[url]http://slideplayer.pl/12279891/72/images/3/Data.List+Intersperse+dostaje+znak+oraz+listę,+a+następnie+umieszcza+znak+pomiędzy+każdą+parą+elementów+na+liście..jpg[/url]
http://images.slideplayer.com/27/8910338/slides/slide_6.jpg

- At the moment StrHatch looks like the best choice. Has that been used in any programming language?
- Normally choosing function names is relatively easy.
User avatar
jeeswg
Posts: 4952
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

05 Sep 2018, 23:50

STRHATCH

StrHatch. My current choice. Hatching means to add parallel lines when drawing, which is the only nearby word I could think of.

hatching - Wiktionary
https://en.wiktionary.org/wiki/hatching#English

STRDELIMIT

StrDelimit has a certain elegance. The only problem being that delimit does not necessarily imply regular intervals.

So if something like this was ever added to AutoHotkey, it could be a good candidate.

(The name surprised me a bit at first, because you usually hear of 'delimiters' in the context of text being divided into separate entities, instead of one item being modified, however, once the dust settles, the name can be appreciated.)

(Also StrAddDelimiters would be an alternative more descriptive name, but that would be too long, so StrDelimit would be better.)

delimit - Wiktionary
https://en.wiktionary.org/wiki/delimit
delimiter - Wiktionary
https://en.wiktionary.org/wiki/delimiter
Delimiter - Wikipedia
https://en.wikipedia.org/wiki/Delimiter
delimit | Hugo
https://gohugo.io/functions/delimit/

comma-separated list
pipe-separated list
delimiter-separated list

Excel: Data, Text to Columns, Delimited
Excel: Data, Text to Columns, Fixed width

If anyone ever does come across an existing function in a programming language that splits by every n characters, that would be great to know. This had even been suggested as a feature in an AHK v2 discussion.

AutoHotkey v2 Alpha Release - Page 32 - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/65311-autohotkey-v2-alpha-release/page-32#entry443330

STRPAD

I will use the name 'StrPad' for a function to add text to the start/end of string, a bit like the Format function.

Links to any existing such functions would be welcome.
User avatar
jeeswg
Posts: 4952
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

06 Sep 2018, 00:30

I've created a 'StrPad' function, for adding characters to the start/end of a string.

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

Last edited by jeeswg on 06 Sep 2018, 18:18, edited 1 time in total.
burque505
Posts: 544
Joined: 22 Jan 2017, 19:37

Re: slice string, pad characters

06 Sep 2018, 12:59

Thanks, jeeswg, that is a nice function.
Regards,
burque505

Return to “Scripts and Functions”

Who is online

Users browsing this forum: DuyMinh, leosouza85, robodesign and 15 guests