strings.htmlTEXTMPadʵee} MathPad Manual _ Strings

Strings


String Constants

Text strings are used for file names and plot labels. Strings are enclosed in double quotes and can contain blanks or punctuation but they cannot contain a RETURN.

Variables can be defined or assigned as strings. If the variable is to be evaluated many times it is better to assign the string so that multiple copies of the string do not need to be created.

sprintf

Numeric results can be converted to strings with the built-in function sprintf(format,n). This function is essentially the same as the C standard library function.

The format parameter is a string which controls the conversion. The format must contain a single conversion specification. Any characters that are not part of the conversion specification are copied directly to the result string. A conversion specification begins with a % character. A % character can be ouput by using %%.

The conversion has the following form: (brackets indicate optional values).

 %[flags][size][.precision]conversion
flag characters
-left justify
+include leading plus
spaceinclude a leading space
0pad with leading zeros
#use alternate form

size
minimum number of output characters
The field is padded with spaces if needed

precision
floating pointnumber of digits after the decimal point
integerminimum number of digits
stringmaximum number of characters

conversion
character n output # alternate form
c ascii codesingle character
d integersigned integer
e E floating pointE formatalways include point
f floating pointfixed point formatalways include point
g Gfloating pointsmaller of f or e include trailing zeros
i integersigned integer
o integeroctalforce leading zero
s stringstringnot allowed
u integerunsigned integer
x Xintegerhexadecimaluse 0x prefix

examples
format sprintf(format,42)
"n=%f"
"n=42.000000"
"n=%e"
"n=4.200000e+01"
"n=%E"
"n=4.200000E+01"
"n=%g"
"n=42"
"n=%.2f"
"n=42.00"
"n=%10.2f"
"n=     42.00"
"n=%-10.2f"
"n=42.00     "
"n=%d"
"n=42"
"n=%X"
"n=2A"
"n=%o"
"n=52"
"n=%c"
"n=*"

Concatenation

The sprintf function only allows one conversion in the format string but results can be joined together with the concatenation operator '|' . Both operands must be strings.

"what" | "not":"whatnot"

String Output

String results are shown in quotes. The table command can be used to output arrays with string elements.
weekdays = {"mon","tues","wed","thurs","fri"}

table {weekdays}
    "mon"    "tues"    "wed"     "thurs"    "fri"

The write() function will write out strings for the special case of a 2D array of strings and a TEXT file. Each string is written out with no quotes or separators. A line in the output file corresponds to a row in the 2D array.

An explicit index on a string will extract the ASCII value of a single character

"tues"[1]:116 -- 116 is the ASCII code for 't'
"tues"[2]:117 -- 117 is the ASCII code for 'u'

Strings are not general purpose objects. They are handled in the above special cases. Most other operations will have no effect on the string and will not generate any error messages. In cases where a string is not allowed, an undefined or an array of ASCII values is used. ddf ?5ȮQH,)Monaco9Monaco9$;??Q?qrNddf3ZPRefSTR#&SPRF2$ < font & size