Dear all
My question might be a silly one, but I am having trouble finding the answer to it.
I have a msgbox that displays a file path, and I want that path to be line-split only at the '/' character, i.e. the character that divides directories from each other (and the final directory from the leafname). How to do that, please?
Here is the sort of thing that I see currently and want to avoid:
[EDIT: the screenshots feature some instances of the wrong type of slash. But you get the idea.]
By contrast, the following is fine (but I see that only because, here, no line splitting at all is necessary):
MsgBox - how to split a long string across lines only at '/'?
-
- Posts: 62
- Joined: 02 Jan 2018, 15:55
Re: MsgBox - how to split a long string across lines only at '/'?
Code: Select all
#Requires AutoHotkey v2.0
str := 'asdf\aspldkfj\alspkdjf\palskjdf\alpsk\fapsldjf\/palskjfdp\laskdjf\palskdjfqqq\palskdjf\palskdjf\alpskdfj\asdkplf'
MsgBox StrReplace(str, '/', '/`n')
-
- Posts: 62
- Joined: 02 Jan 2018, 15:55
Re: MsgBox - how to split a long string across lines only at '/'?
Thanks. That - or rather StrReplace(str, '\', '\`n') produces the likes of this:
That is not exactly what I want. I will investigate `StrReplace` and see what I can come up with.
EDITED to fix formatting.
That is not exactly what I want. I will investigate `StrReplace` and see what I can come up with.
EDITED to fix formatting.
Last edited by UserNameForAH_Board on 05 May 2024, 09:12, edited 1 time in total.
Re: MsgBox - how to split a long string across lines only at '/'?
It seems that the script that I posted is the one that you want.
What the script does:
What the script does:
If not, you can use other characters, examine substrings, and check the length of these strings to act accordingly. If your string exceeds a certain length and contains a certain character in the substring that does not exceed that length, you can insert a line feed after the final occurrence of the character in the substring.How to split a long string across lines only at '/'?
-
- Posts: 62
- Joined: 02 Jan 2018, 15:55
Re: MsgBox - how to split a long string across lines only at '/'?
Thanks. Your code splits the string only and always at \. I do not want the always. But I think you've given me enough to work with. So, as I say: thank you.
Re: MsgBox - how to split a long string across lines only at '/'?
My StrReplace contains no backslashes, but if you have it working, then it's all fine.
Re: MsgBox - how to split a long string across lines only at '/'?
Also, depending on what you are trying to accomplish, this might be helpful.
https://www.autohotkey.com/docs/v2/lib/SplitPath.htm
https://www.autohotkey.com/docs/v2/lib/SplitPath.htm
name := "ste(phen|ve) kunkel"
Re: MsgBox - how to split a long string across lines only at '/'?
Code: Select all
#Requires AutoHotkey v2.0
str := 'asdf\aspldkfj\alspkdjf\palskjdf\alpsk\fapsldjf\/palskjfdp\'
. 'laskdjf\palskdjfqqq\palskdjf\palskdjf\alpskdfj\asdkplf'
MsgBox split(str, 65), 'Output', 'Iconi'
split(str, maxi) {
Static regex := '[/\\]'
If StrLen(str) > maxi && str ~= regex {
If !pos := RegExMatch(SubStr(str, 1, maxi), '.+\K' regex)
pos := RegExMatch(str, regex)
Return SubStr(str, 1, pos) '`n' split(SubStr(str, pos + 1), maxi)
} Else Return str
}
Re: MsgBox - how to split a long string across lines only at '/'?
Another option: Instead of using a MsgBox, create a simple GUI to display your message so you don’t have to worry about it breaking in the wrong place because you can have it not break at all.
Re: MsgBox - how to split a long string across lines only at '/'?
An example of boiler's idea is below.
Code: Select all
#Requires AutoHotkey v2.0
str := 'asdf\aspldkfj\alspkdjf\palskjdf\alpsk\fapsldjf\/palskjfdp\'
. 'laskdjf\palskdjfqqq\palskdjf\palskdjf\alpskdfj\asdkplf'
box(str)
MsgBox 'Done!', 'Status', 'Iconi'
box(str) {
Try g.Destroy
Global g := Gui('-MinimizeBox', 'Output')
g.SetFont 's10'
g.AddText 'y20', str
btn := g.AddButton('w113 y+20 Default', 'OK')
btn.OnEvent('Click', (btn, info) => btn.Gui.Destroy())
g.Show('Hide'), g.GetPos(,, &gw), btn.GetPos(,, &bw), btn.Move((gw - bw) / 2)
g.Show(), WinWaitClose(g)
}
-
- Posts: 62
- Joined: 02 Jan 2018, 15:55
Re: MsgBox - how to split a long string across lines only at '/'?
Thanks, all.
I think I've got it working. I had a lot of trouble working out how the logic should be and I'm unsure I could sum up the logic that I have implemented! Here though is the relevant part of my code.
I think I've got it working. I had a lot of trouble working out how the logic should be and I'm unsure I could sum up the logic that I have implemented! Here though is the relevant part of my code.
Code: Select all
local displayStr, maxLen
path_display := path
maxLen := 30
if StrLen(path_display) > maxLen {
; response := MsgBox("String needs wrapping. Length: " . StrLen(path_display) . ". Str: " . path, progName, "YNC Icon?")
; switch response {
; case "No": return
; case "Cancel": exitApp
; }
local n, position, workingStr
n := 1
workingStr := path_display
; InStr syntax: FoundPos := InStr(Haystack, Needle , CaseSense, StartingPos, Occurrence)
loop {
if not position := InStr(path_display, '\' , , , n)
break
workingStr := SubStr(path_display, 1, position)
; response := MsgBox("Iteration: " . n . ". Position: " . position . ". Working string: " . workingStr, progName, "YNC Icon?")
; switch response {
; case "No": return
; case "Cancel": exitApp
; }
n++
} until StrLen(workingStr) >= maxLen
path_display := workingStr . "`n" . SubStr(path, position + 1)
}
; Make spaces in path-for-display non-breaking.
path_display := StrReplace(path_display, ' ', ' ')
Who is online
Users browsing this forum: DavidP and 86 guests