March 2019: ewertb.soundlinker.com is getting relocated to ewertb.mayasound.com, please update your bookmarks.

MEL How-To #11

Back · Previous · Next Maya

How do I strip the "new-line-character" off of the end of my string variable?

When reading a DOS file where each line ends with a CR+LF pair, the ‘fgetline’ command knows to strip the LF, but fails to string the CR. This is a problem I've run into as well.

You can use the ‘match’ command with a regular expression to strip a CR and/or LF from the end of a text string.

The escape sequence that matches a LF is "\n", that which matches a CR is "\r". Armed with this:

$string = match( "^[^(\r\n)]*", $string );

What this does is matches all characters starting at the beginning of a string which is not a CR or LF character, reassigning the results to $string. This command is quick to execute, and safe to use even if there are no LF or CR characters in the string.