PDA

View Full Version : Change characters


brm
07-20-2005, 11:55 PM
I would insert the date in the file name drawings.
(example 21-Jul-2005 9:38:20)
Is there a simple way to replace the ':' with '-'.

John Scheffel
07-21-2005, 12:49 PM
I'm not aware of any character or string replace command in Drafting, but you can do it by parsing the text and using substrings with the following macro.
DEFINE Date_with_dashes
LOCAL Pos_colon
LOCAL Len_remainder

LET My_date (DATE)
LET Pos_colon (POS My_date ':')
LET Len_remainder ((LEN My_date) - Pos_colon)
LET My_date ((SUBSTR My_date 1 (Pos_colon - 1)) + '-'
+ (SUBSTR My_date (Pos_colon + 1) Len_remainder))
LET Pos_colon (POS My_date ':')
LET Len_remainder ((LEN My_date) - Pos_colon)
LET My_date ((SUBSTR My_date 1 (Pos_colon - 1)) + '-'
+ (SUBSTR My_date (Pos_colon + 1) Len_remainder))
DISPLAY (My_date)
END_DEFINE

You can remove the DISPLAY line and use the My_date variable where needed.

John Scheffel
07-21-2005, 02:57 PM
After posting the above reply, I started thinking that it might be nice to have a more general macro to do this kind of string replacement. I came up with the following macro which should work for replacement of single characters or multi-character substrings, they don't even have to be the same length.
DEFINE Replace_string_in_string
PARAMETER Target_string {String to replace sub-strings in}
PARAMETER Old_string {sub-string to replace}
PARAMETER New_string {sub-string to replace with}

LOCAL Pos_old_string
LOCAL Len_old_string
LOCAL Len_remainder

LET Len_old_string (LEN Old_string)
WHILE (POS Target_string Old_string)
LET Pos_old_string (POS Target_string Old_string)
LET Len_remainder ((LEN Target_string) - (Pos_old_string + Len_old_string) + 1)
LET Target_string ((SUBSTR Target_string 1 (Pos_old_string - 1))
+ New_string
+ (SUBSTR Target_string (Pos_old_string + Len_old_string) Len_remainder))
END_WHILE
DISPLAY (Target_string)
LET Return_string Target_string
END_DEFINE
I haven't done much testing of it, but it seems to work. So for example, you could modify the date by calling.

Replace_string_in_string (DATE) ':' '-'

The variable Return_string will then contain the date in the format you wanted.