Department of Mathematics

Math 300: Mathematical Computing

Maple Strings

Maple handles strings very much like other programming languages. Indeed, Maple's string handling is much more like that of ordinary programming languages than Matlab's.

The first thing to note about Maple strings is that they are enclosed in double quotes. Single quotes represent unevaluation - something entirely different; you should never use single quotes for character strings. Maple has a sprintf command to format strings, similar to every other language. It has form sprintf(format string, variables). It uses the usual formatting specifications: %d to represent an integer, %f to represent a floating-point number, and %s to represent a string input.

n:=4: nf:=exp(n); degstr:=sprintf("The power of the exponential is %d",n); degstr := "The power of the exponential is 4" valstr:=sprintf("The value of the exponential is %f",n); valstr := "The value of the exponential is 4"

There is another function called printf that does almost the same thing, except that it not only creates the string, but prints it out. Use sprintf when you want to make a string using some worksheet data, but use it later. Use printf when you just want to print some output immediately.

You can concatenate strings using the cat function, and you can convert strings to variable names using the convert function.

hi:=cat("Hello"," world"); hi := "Hello world" x1:=42: strs:=cat("x",1..4); strs := "x1","x2","x3","x4" new1:=convert(strs[1],name); x1 new1; 42

Assignment 7 is posted.

A solution to the test is available.

Department of Mathematics, PO Box 643113, Neill Hall 103, Washington State University, Pullman WA 99164-3113, 509-335-3926, Contact Us
Copyright © 1996-2015 Kevin Cooper