Page 1 of 1

Stringleft, stringright and substr: ALLOW PAD string

Posted: 22 Feb 2018, 10:45
by joefiesta
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.

Re: Stringleft, stringright and substr: ALLOW PAD string

Posted: 22 Feb 2018, 14:02
by nnnik
Isn't Format for formatting strings?

Re: Stringleft, stringright and substr: ALLOW PAD string

Posted: 22 Feb 2018, 14:17
by joefiesta
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).

Re: Stringleft, stringright and substr: ALLOW PAD string

Posted: 22 Feb 2018, 15:04
by nnnik
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?

Re: Stringleft, stringright and substr: ALLOW PAD string

Posted: 22 Feb 2018, 18:09
by jeeswg
- 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)
}

Re: Stringleft, stringright and substr: ALLOW PAD string

Posted: 23 Feb 2018, 21:10
by kczx3
I do agree that the Format function is extremely complicated and hard to understand how to use it.

Re: Stringleft, stringright and substr: ALLOW PAD string

Posted: 23 Feb 2018, 22:25
by jeeswg
- @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.