Counting line feeds and remove unwanted string content

Counting line feeds and remove unwanted string content

Browning_Zed
Advocate Advocate
535 Views
4 Replies
Message 1 of 5

Counting line feeds and remove unwanted string content

Browning_Zed
Advocate
Advocate

Greetings,
I have a question related to counting the number of line feed in a string contained in the Text / MText / MLeader and defining function based on these data.
As far as I know, a line feed in AutoLISP can be denoted by two characters:
"\n" (is equivalent to 10 ASCII character)
or
"\\P" - for strings contained in the Text / MText / MLeader (is equivalent to 92 and 80 ASCII characters following each other)
Thus, the number of line feed can be calculated by counting the number of characters "\n" and "\\P" contained in the string.
Now I need a function that, based on counted line feeds, would return a certain result:
• if the string does not contain line feed characters then return nil:

 

"Apple" >>> nil

 

• if the string contains one line feed, that is, a line break in a string into two lines (the top line may be empty) then return this string:

 

"Apple\\PBanana" >>> "Apple\\PBanana"
"\nApple" >>> "\nApple"

 

• if there is more than one line feed in the string then it is required to keep the top two lines in string and delete the other content of the string, then return the transformed string.

 

"Apple\\PBanana\nOrange" >>> "Apple\\PBanana"
"\nApple\\PBanana\nOrange" >>> "\nApple"

 

Any help would be greatly appreciated.
Thank you in advance.

0 Likes
536 Views
4 Replies
Replies (4)
Message 2 of 5

john.uhden
Mentor
Mentor

@Browning_Zed 

At first I was thinking you wanted just to count the line feeds, either hard or soft.

This will do that:

(defun getfeeds (str / p n feeds)
  (setq p 0 n 0 feeds 0)
  (while (or p n)
    (if (setq p (vl-string-search "\\P" str p))
	  (setq feeds (1+ feeds) p (1+ p))
	)
    (if (setq n (vl-string-search "\\n" str n))
	  (setq feeds (1+ feeds) n (1+ n))
	)
  )
  feeds
)

But it seems you want to remove everything after the last line feed (either "\\P" or "\\n"), so...

(defun remove_after_last_feed (str / p n pmax nmax)
  (setq p 0 n 0)
  (while (or p n)
    (if (and p (setq pmax p)(setq p (vl-string-search "\\P" str p)))
	  (setq p (1+ p) pmax p)
	)
    (if (and n (setq nmax n)(setq n (vl-string-search "\\n" str n)))
	  (setq n (1+ n) nmax n)
	)
  )
  (substr str 1 (1- (max pmax nmax)))
)
;; which may return "" if there are no feeds, but we can fix that if you want

 

John F. Uhden

Message 3 of 5

Browning_Zed
Advocate
Advocate

Hi john.uhden!
Thanks for your help and your time for me.

Function remove_after_last_feed works great if string contains more than one "\n" or "\\P" character.
But if a string consists of two lines, and the first or second line can be "empty" - such a string can be seen if the following expression is applied to the MText entity:
(vla-put-textstring (vlax-ename->vla-object (car (entsel))) "\nApple")
(vla-put-textstring (vlax-ename->vla-object (car (entsel))) "Apple\n ")
I would like the function to return a string in this case as well:

 

"\nApple" >>> "\nApple"
"Apple\n " >>> "Apple\n "
"\\PApple" >>> "\\PApple"

 

but in its current form, the function returns: bad argument value.
Also, if the string does not contain a line feed the function will return bad argument value, but I would like it to return nil or string (which will be easier to do):

 

"Apple" >>> nil
or
"Apple" >>> "Apple"

 

 

Thanks again for your help.

0 Likes
Message 4 of 5

Browning_Zed
Advocate
Advocate

I tried to write a line feed counting function:

 

(defun LineFeedCount ( str / i )
	(setq i 0)
	(foreach n
		(vl-string->list
			(vl-string-subst "" "P"
				(vl-string-translate "\\P" "\n" str)
			)
		)
		(if (equal n 10) (setq i (1+ i)))
	)
	i
)

 

Now the function:

(defun remove_after_last_feed (str / p)
	(setq p 0)
	(while p
		(if (and p (setq p (LineFeedCount str)))
			(setq p (1+ p))
		)
	)
	(substr str 1 (1- p))
)

is in an endless loop.

How do I get this to work?

0 Likes
Message 5 of 5

john.uhden
Mentor
Mentor
@Browning_Zed,
Lemme see.
So if there is only one \\n or \\P, you want it preserved?
And if it's an Apple, it must be retained no matter what?
Okay, so whether it's an apple or an orange, you want to retain the
following line feed if it follows just one word?
And it seems you want to always retain a beginning line feed, but remove
after the 2nd line feed?
Do you want to retain the last line feed but remove the text after it?
I am having a tough time understanding the rules of your game and what
purpose it serves. Like how did the objectionable text get created in the
first place? Did your company suddenly impose a new standard?
If so, then why are your people ignoring it? Maybe instead of my time, you
need some enforcement. Like have them fix these things by hand on their
own time.

John F. Uhden

0 Likes