Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to remove excess lines
#1
i have multi-line string that i want to remove the excess lines and just leave 1 double space. i have tried the findreplace("[] []" "[]". thinking it would remove double carriage and leave 1 but it removes ALL carriage returns/new lines. i want to leave 1 line space between strings

EXAMPLE
dog jump over the fence



the cat meows


look both ways before crossing

SHOULD BE...
dog jump over the fence

the cat meows

look both ways before crossing
#2
this works
s.findreplace("[][]" "[]")
Code:
Copy      Help
str s=
;dog jump over the fence
;
;
;the cat meows
;
;
;look both ways before crossing
;
;;

out numlines(s)
s.findreplace("[][]" "[]")
out numlines(s)
out s
 this doesn't 
s.findreplace("[] []" "[]")
nor does 
s.findreplace("[][]" "[]" 8)
#3
thanks kevin.  but this fails if the spacing or carriage returns isnt exactly 2 or less... my examples has 3 and 2, my real world has 3, 6, 4. carriage returns/new lines between strings....
Code:
Copy      Help
out
str a
str s="one[][][][][][]two[][][][][]yes"

s.findreplace("[][]" "[]")
out s
,
#4
Try this function:
Function TrimMultiBlankLines
Code:
Copy      Help
;Reduce multi blank lines into one blank line only.

function str'text
str line
int emptyLineCount emptyLineExist
out
foreach line text
,line.trim()
,if (len(line) = 0)
,,emptyLineCount +=1
,,emptyLineExist = 1
,,if (emptyLineCount > 1)
,,,emptyLineExist = 0
,else
,,out line
,,emptyLineCount = 0
,if (emptyLineExist )
,,out ""
,,emptyLineExist = 0

Usage: TrimMultiBlankLines [string_text]
#5
replacerx using regular expressions would probably be best here
Code:
Copy      Help
out
str s="one[][][][][][]two[][][][][]yes[][][][][][][][]No[][]End"
s.replacerx("[\r\n]+" "[][]")
out s


Forum Jump:


Users browsing this thread: 1 Guest(s)