We help IT Professionals succeed at work.

Remove empty space in sourcescode inside editor

Stef Merlijn
Stef Merlijn asked
on
Medium Priority
115 Views
Last Modified: 2019-10-16
Hi,

I have al lot of coded strings that look like below.
  '     END                  ' + chr(13) +
  '   ELSE ' + ''MyVar'' + '                           ' + chr(13) +

Open in new window

What I would like to accomplisch is to remove all spaces, except one, before ' + chr(13) +. The number of spaces will differ per line of code.
Result should be:
  '     END ' + chr(13) +
  '   ELSE ' + ''MyVar'' + ' ' + chr(13) +

Open in new window


Is there an easy way to do this automatically inside the Delphi Editor (maybe some macro?), or is there another solution?
Thanks!
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2014

Commented:
Your desired result has an odd number of apostrophe characters.
Stef MerlijnDeveloper

Author

Commented:
I've adjusted the strings, making an example was aparently too hard for me :-)
CERTIFIED EXPERT
Top Expert 2014

Commented:
I used Notepad++ and did a replace all of this regex pattern:
  +'

Open in new window

with this result:
 '

Open in new window

The replacement operation replaces two or more space characters before an apostrophe character with a single space character followed by an apostrophe.

I don't know what editor you are using or if it features regex pattern matching.
Senior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013
Commented:
YOu may not need regex though that is the easiest way.

Try replacing
  ' + chr(13)
with
 ' + chr(13)

The first has 2 spaces at the left and the replacement has only 1 space

repeat until there are no matches

mlmcc
Stef MerlijnDeveloper

Author

Commented:
@aikimark: Thanks.

@Mike McCracken: Great idea, I should have thought of it myself :-)
It's perfect because I can easily repeat the replacement via F3 key in next unit. And it can be used inside the Delphi editor.

Thank you.
Sinisa VukSoftware architect
CERTIFIED EXPERT
Top Expert 2012

Commented:
I would to suggest not to use spaces in a code like that. If you have so much space strings - use StringOfChar function....

  ...  StringOfChar(' ', 4) + 'END' + StringOfChar(' ', 2) +MyVar + StringOfChar(' ', 12) + chr(13) + ...

Open in new window


length parameter in StringOfChar can be also programmatically controled (variable also)
Stef MerlijnDeveloper

Author

Commented:
@Sinisa Vuk: Thank you for your suggestion. Much appreciated!