Escape Sequences

You've seen "\n" used to create a newline.  That's a simple escape sequence.
More complex sequences can be necessary when assembling strings for certain usages.
For example, to create a simple statement to be executed with eval, you may do this:

{
	string $cmd = "print \"I am printing in a crazy way!!!\n\"";
	eval($cmd);
}

The result should be:
// Error: print "I am printing in a crazy way!!! //
// Error: Line 1.39: Unterminated string. //

The new line needs to be double escaped, because when maya passes the string to the eval command
it sort of dequotes the string one level:

{
	string $cmd = "print \"I am printing in a crazy way!!!\\n\"";
	eval($cmd);
}

...fixes it.

That was simple enough.

A more legitimate usage would be to create an expression node from a script.

{
	string $sp[] = `sphere -ch 0`;
	string $exp = (
		"{"
			+"\t" + $sp[0] + ".translateY = " + $sp[0] + ".translateX;\n"
		+"}"
	);
	
	expression -s $exp;
}


That works fine, but what if for some reason you needed to execute that
from an eval command?
It may not seem too likely, but it can happen.
Double escaping that whole piece doesn't seem like fun, and a larger script
would be even worse.

Here's a shortcut for whenever you need to do multiple escapes on a large string.
You can do something like this:
this example uses: file I/O
The script is too large so download it here
(right click, save Target As if it tries to open in the main window.
String Encode File

The procedure being used as an example is at the top.
All it does is open the file, read in a line, run encodeString on the line, and print
the results out to a new file.

The new file has the same name as the original, but with _Encoded following it.
The new file should be all on one line, and look really ugly.
You could go through the line, and break it up wherever you want and use string concatenation
to piece it together, or you could improve the script to do it for you.
More on that in a later tutorial though.