Stringleft, stringright and substr: ALLOW PAD string

Propose new features and changes
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Stringleft, stringright and substr: ALLOW PAD string

22 Feb 2018, 10:45

Revised commands STRINGLEFT, STRINGRIGHT and SUBSTR() function.

I would like to see these handle an additional PAD parameter. The new syntax being:
StringLeft, OutputVar, InputVar, Count, Pad
StringRight, OutputVar, InputVar, Count, Pad
NewStr := SubStr(String, StartPos [, Length[,Pad]])


For the commands, when Count is greater than the length of InputVar, OutputVar would be padded to length of Count with n Pad characters. For StringRight, the padding would prefix the output variable.

For Substr(), when StartPos is negative and Length exceeds the number of characters available, the pad would PREFIX the output.

In all these cases, the implemention should probably allow Pad to be a string instead of single character.


Rationale:

a. This would be entirely backward compatible.

b. MSGBOX or a simple GUI of text is rather clumsy when it comes to displaying columnar data because of the inability (as far as I know) of defining tab stop positions. One could easily use a FIXED font and pad data fields to align nicely. (Yes, defining tab stops in GUIs would be awesome, but I suspect well beyond the scope of something so simple as my proposal!)

c. Yes, I can do something like this:

Code: Select all

  var := "my string"
pad := "---------------------------------------------------------"
padded_var := substr(var . pad, length)
but just the idea of "pad :="--------------------------------------------" is appalling.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Stringleft, stringright and substr: ALLOW PAD string

22 Feb 2018, 14:02

Isn't Format for formatting strings?
Recommends AHK Studio
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Stringleft, stringright and substr: ALLOW PAD string

22 Feb 2018, 14:17

I would say NO. I can't figure out how to use format()to pad with anything other than a space. It's documentation only mention padding in 2 regards: (1) padding with spaces and (2) suppressing padding.

In addition, I find the format() function to be absurdly complicated (Yes, if you need all that, there it is), while what I am suggesting is virutally trivial.

If format() can pad with something other than spaces, please show me how. IMHO format() is for numerics and just numerics (although, yes, it can handle strings).
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Stringleft, stringright and substr: ALLOW PAD string

22 Feb 2018, 15:04

Well as far as I understood, the job of StrRight/Left or SubStr is to cut out a part of a string.
How exactly does padding the string with something play a role in that task?
Recommends AHK Studio
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Stringleft, stringright and substr: ALLOW PAD string

22 Feb 2018, 18:09

- I think that the best solution for this problem would be a built-in StrRept function.
- For displaying the text, you could use a custom GUI with an equal-width font, e.g. Courier New, or the Progress command, or change the font for MsgBox temporarily.
[JEE_SystemSetFont function]
GUI COMMANDS: COMPLETE RETHINK - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 78#p131078
- I'm working on alternatives to the MsgBox/InputBox commands, with added functionality, including specifying a font.

Code: Select all

q:: ;crop and pad

;pad
vText := "abc"
MsgBox, % "[" Format("{:10}", vText) "]"
MsgBox, % "[" Format("{:-10}", vText) "]"
MsgBox, % StrReplace(Format("{:10}", vText), " ", "_")
MsgBox, % StrReplace(Format("{:-10}", vText), " ", "_")

;pad
vText := "abc"
MsgBox, % JEE_StrRept("_", 10-StrLen(vText)) vText
MsgBox, % vText JEE_StrRept("_", 10-StrLen(vText))

;crop and pad
vText := "abcdefghijklmnopqrstuvwxyz"
MsgBox, % JEE_StrRept("_", 10-StrLen(vText)) SubStr(vText, 1, 10)
MsgBox, % SubStr(vText, StrLen(vText)+1-10) JEE_StrRept("_", 10-StrLen(vText))
;MsgBox, % JEE_SubStr(vText, -10) JEE_StrRept("_", 10-StrLen(vText))

;[JEE_SubStr function]
;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689
return

JEE_StrRept(vText, vNum)
{
	if (vNum <= 0)
		return
	return StrReplace(Format("{:" vNum "}", ""), " ", vText)
	;return StrReplace(Format("{:0" vNum "}", 0), 0, vText)
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Stringleft, stringright and substr: ALLOW PAD string

23 Feb 2018, 21:10

I do agree that the Format function is extremely complicated and hard to understand how to use it.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Stringleft, stringright and substr: ALLOW PAD string

23 Feb 2018, 22:25

- @kczx3: I agree, although with a good list of examples, I don't think it's too bad. See 'FORMAT FUNCTION EXAMPLES' here:
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 72#p124372
- The key things to be aware of are: (1) each {} / {:f} / {n} / {n:f} contains text based on the parameters, the rest of the text is literal text, (2) {n:f}, n is a parameter number, f is a format.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 21 guests