Break text into new lines

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
KilliK
Posts: 255
Joined: 10 Mar 2016, 21:19

Break text into new lines

19 Feb 2018, 07:01

Hello.

I have this text which is basically one line:
--AAA-BBB--CCC-DDD--EEE-FFF...etc

I am trying to break it so that each part which starts with -- is put in a new line.
Ι want to look like this:
--AAA-BBB
--CCC-DDD
--EEE-FFF
...and so on

I am trying to do it with the below code but with no success.

Code: Select all

loop, parse, textstring, --
newstring .= A_LoopField . "`r`n"
can someone help me with this?
Ravenlord
Posts: 14
Joined: 19 Jan 2018, 03:43

Re: Break text into new lines

19 Feb 2018, 07:14

Code: Select all

StringReplace, textstring, textstring, --, `n, All
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Break text into new lines

19 Feb 2018, 07:18

Some ideas:

Code: Select all

q:: ;split text by '--', using RegExReplace
vText := "--AAA-BBB--CCC-DDD--EEE-FFF"
;[^a] means not a, in RegEx, text between square brackets is a character class
;[^\-] means not -, - is escaped with \ because - has a special meaning within a character class e.g. [a-z] means all of the characters from a to z
;$1 contains whatever is within the first set of parentheses
vText := RegExReplace(vText, "([^\-])--", "$1`r`n-")
MsgBox, % vText
return

w:: ;split text by '--', using StrReplace
vText := "--AAA-BBB--CCC-DDD--EEE-FFF"
vText := StrReplace(vText, "--", "`r`n--")
vText := LTrim(vText, "`r`n")
MsgBox, % vText
return

e:: ;split text by '--', using StrSplit
vText := "--AAA-BBB--CCC-DDD--EEE-FFF"
oArray := StrSplit(vText, "--")
vText := ""
for vKey, vValue in oArray
	vText .= (A_Index=1?"":"`r`n--") vValue
vText := LTrim(vText, "`r`n")
MsgBox, % vText
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Break text into new lines

19 Feb 2018, 11:45

another example

Code: Select all

x:="--"
string=--AAA-BBB--CCC-DDD--EEE-FFF
a:=StrSplit(String,x)
Loop % a.MaxIndex()
   c .= If (A_Index =a.maxindex() ) ? a[a_index] . "`n" : a[a_index] . "`n--"
msgbox,%c%
User avatar
KilliK
Posts: 255
Joined: 10 Mar 2016, 21:19

Re: Break text into new lines

21 Feb 2018, 07:46

thank you for your help, I ll test your code asap. thank you again.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, mikeyww, scriptor2016 and 278 guests