Most built-in functions and operators do not make sense when used with strings and will issue error messages if you use string arguments with them. Mathcad Professional, however, includes several specialized functions for working with strings.
The strings used and returned by most of the functions described below are typed in a math placeholder by pressing the double-quote key (n) in a placeholder and entering any combination ofletters, numbers, or other ASCII characters. Mathcad automatically places double quotes around the string expression, and will display quotes around a string whenever one is returned as a result.
See Chapter 8, “Variables and Constants,” for more information on strings.
The following four functions are useful for combining strings, determining the length of a string, and locating and returning substrings.
When evaluating the functions search and substr, Mathcad assumes that the first character in a string is at position O
Converting to and from strings
Use num2str and str2num to convert between numeric values and strings. These functions are useful when it is more convenient to manipulate a number as a string rather than mathematically, or when you need to perform numerical calculations on a string of numbers.
The str2num function requires a string consisting of characters which constitute an integer, a floating-point or complex number, or an e-format number such as 4.51e-3 (for 4.51· 10-3 ). For example, valid string arguments for str2num are “-16.5,” “2.1+6i,” “3.241 – 9.234j,” and “2.418e3.” Spaces are ignored.
Use str2vec and vec2str if you want to convert between the ASCII codes for characters and .tj1echaracters themselves. For a list of ASCII codes see Appendix A, “Reference.” For example, the ASCII code for letter “a” is 97, that for letter “b” is 98, and that for letter “c” is 99.
Customized error messages
Just as Mathcad’ s built-in error messages appear as “error tips” when a built-in function is used incorrectly or could not return a result, you may want specialized error messages to appear when your user-defined functions are used improperly or cannot return answers.
Mathcad allows you to define your own error messages using the string function error. This function is especially useful for trapping erroneous inputs to Mathcad programs you write. See Chapter 18, “Programming,” for details on using error in programs.
Returns the string S as an error message.
When Mathcad encounters the error function in an expression, it highlights the expression in red. When you click on the expression, the error message appears in a tool tip that hovers over the expression. The text of the message is the string argument you supply to the error function.
Figure 13-8 shows examples of Mathcad’s string manipulation, string conversion, and custom error message functions