Jump to content


Photo

[SOLVED]Insert linebreaks every 2 characters?


  • Please log in to reply
6 replies to this topic

#1 akadewboy

akadewboy
  • Members
  • 15 posts

Posted 01 June 2012 - 12:44 PM

I have a txt file that looks kinda like this:

A
dragon is here.
Take this
sword.
Good Luck.
bye

I need it to look like this:

A
dr
ag
on
 i
s 
he
re
.
Ta
ke
 t
hi
s
sw
or
d.
Go
od
 L
uc
k.
by
e

I have a feeling I just need a simple loop, but I'm not sure what to use.

#2 Guests

  • Guests

Posted 01 June 2012 - 12:51 PM

You can use TF if you wish <!-- m -->http://www.autohotke...lib.htm#TF_Wrap<!-- m -->
(no need for a loop, just a RegExReplace will do, TF will save you the read, adjust, save trouble)

#3 akadewboy

akadewboy
  • Members
  • 15 posts

Posted 01 June 2012 - 01:07 PM

I tried TF.ahk and it didn't turn out right:

A
Dr
ag
on
 i
s 
he
re
. 
Ta
ke
 t
hi
s 
sw
or
d.
 
He
 c
an
 b
e 
fo
un
d 
in
 t
he
 
ca
ve
 t
o 
th
e 
ri
gh
t.
 
Go
od
 l
uc
k.
 
Be
 c
ar
ef
ul
. 

Notice how after "sword.", "the", "right.", and "luck." there's an extra line and a " " was inserted. I can't have extra spaces added to my txt file.

#4 sinkfaze

sinkfaze
  • Moderators
  • 6087 posts

Posted 01 June 2012 - 01:12 PM

var=

(

A

dragon is here.

Take this

sword.

Good Luck.

bye

)

MsgBox %	RegExReplace(RegExReplace(var,".{2}(?!$)", "$0`n"),"\v{2,}","`n")


#5 Hamlet

Hamlet
  • Members
  • 195 posts

Posted 01 June 2012 - 01:14 PM

v =

(

A

dragon is here.

Take this

sword.

Good Luck.

bye

)

Msgbox % RegExReplace( RegExReplace( v, "(..)", "$1`r`n"), "\n")


#6 akadewboy

akadewboy
  • Members
  • 15 posts

Posted 01 June 2012 - 01:16 PM

Thanks, works perfectly now. :D

#7 Guests

  • Guests

Posted 01 June 2012 - 01:19 PM

I think that has to do with the `r`n carriage return/linefeed chars. Take a look at this example and if you comment text:=Re... line you can see the difference
text=

(join`r`n

A

dragon is here.

Take this

sword.

Good Luck.

bye

)



text:=RegExReplace(text,"\r?\n", " ") ; comment this to see the difference

MsgBox % RegExReplace(text,"(.{2})", "$1`n")
So you could try to replace all `r`n with a space and THEN wrap it to get the results you need.