View Full Version : editing text
bryan.thatcher
2009-07-30, 02:26 PM
i need a way to remove all of the characters up to a space. Does anyone have a starting point or an example? Thanks.
ccowgill
2009-07-30, 05:24 PM
i need a way to remove all of the characters up to a space. Does anyone have a starting point or an example? Thanks.
look up vl-string-left-trim in the developer help
(vl-string-left-trim "characters to be removed" string)
msretenovic
2009-07-31, 03:58 PM
Give this a try. It is more general in that you do not have to list every character (and worry about which ones you missed) using vl-string-left-trim.
(defun MKSx-String-Left-Trim
(
str
strDel
/
strNew
)
;;Ensure both parameters are strings
(if (and
(= (type str) 'STR)
(= (type strDel) 'STR)
)
(progn
;;get length of strings
(setq intDel (strlen strDel)
intIdx 1
intStr (strlen str)
)
;;while not at end of string and delimeter not found...
(while (and
(< intIdx intStr)
(/= (substr str intIdx intDel) strDel)
)
;;...increment index
(setq intIdx (1+ intIdx))
);;end while
;;get substring from delimeter to end of string
(setq strNew (substr str (+ intIdx intDel)))
);;end if
);;end if
;;return new string
strNew
)
irneb
2009-08-06, 07:44 PM
If you mean you have a string like "ToRemove ToStay", then you'd do this using 2 functions: 1st using vl-string-search or vl-string-position to get the position of the space, then 2nd using that position in substr as a starting position for the substring. Please note vl-string-XXX all use/give 0 based positions (i.e. the 1st char is at position 0), while substr uses 1 based (i.e. 1st char at pos 1).
Otherwise (the old way of doing it) set a counter to e.g. (setq n 1). Then using a while loop to check if (= " " (substr str n 1)), increment the counter (setq n (1+ n)). Then simply use substr again (substr str n) will give you the entire string starting from the 1st space.
CAB2k
2009-08-09, 07:32 PM
Another version
;; CAB 08.09.09
;; Left trim all up to & including strDel
;; (vl-string-left-trim "abc" "abccbamy string") >---> "my string"
;; (String-Left-Trim "c" "abccbamy string") >---> "cbamy string"
;; (String-Left-Trim "x" "abccbamy string") >---> ""
;; return nil if bad type, "" if not found, else substr
;; Flag for case sensitive
(defun String-Left-Trim (strDel ; string delimiter
str ; string to test
case ; case flag nil = not case sensitive
/ ptr)
(if (= (type str) (type strDel) 'STR) ; verify types
(if (setq ptr (vl-string-search (if case strDel (strcase strDel))
(if case str (strcase str))))
(substr str (+ ptr 2))
""
)
)
)
(defun c:test()
(print (String-Left-Trim "d" "abc1def" nil))
(print (String-Left-Trim "x" "abc1def" nil))
(print (String-Left-Trim "D" "abc1def" T))
(princ)
)
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.