Strings #

Math.js uses JavaScript strings, written with single or double quotes. Use math.string to convert a value to a string. Arrays and matrices are converted element by element.

math.typeOf('hello')       // 'string'
math.string(2.3)           // '2.3'
math.string([2, true])     // ['2', 'true']

Numeric conversion #

Numeric functions such as math.add and math.max attempt to convert string arguments to numbers. Conversion fails if a string cannot be parsed as a number. For example, math.add('hello ', 'world') throws an error.

math.add('2', '3')         // 5
math.max('3', '2', '7')    // 7
math.number('2.3')         // 2.3

Functions with string signatures can handle strings directly. In particular, use math.concat to concatenate text:

math.concat('hello ', 'world') // 'hello world'
math.concat('2', '3')          // '23'

The expression parser uses + for arithmetic, including numeric conversion of strings. JavaScript’s + operator concatenates strings instead:

'2' + '3'                                  // '23'
math.evaluate('"2" + "3"')                 // 5
math.evaluate('concat("hello ", "world")')  // 'hello world'

Other operations #

Some functions listed under Matrix also accept strings: math.count, math.size, math.subset, and math.resize. The JavaScript API uses zero-based indices in math.index; the expression parser uses one-based indices.

math.count('hello')                      // 5
math.size('hello')                       // [5]
math.subset('hello', math.index(1))       // 'e'
math.evaluate('"hello"[2]')               // 'e'
math.resize('hello', [8], '!')            // 'hello!!!'

Comparing and formatting strings #

math.compareText compares text lexicographically. math.compareNatural uses natural ordering, comparing numeric parts by their value:

math.compareText('10', '2')     // -1
math.compareNatural('10', '2')  // 1
math.compareText('file2part10', 'file2part2')     // -1
math.compareNatural('file2part10', 'file2part2')  // 1

Use math.format to format values, or math.print to interpolate values into a string template. See the String functions reference for more functions.

Fork me on GitHub