Built-In Namespace String
Method Attributes | Method Name and Description |
---|---|
$(arg)
An abbreviation for String.form.
|
|
count(regex, case)
Like String match, but return the number counts how often
the regular expression matches.
|
|
<static> |
String.form(format, elements)
Format a string (similar to printf in C).
|
Method Detail
{String}
$(arg)
An abbreviation for String.form.
Converts all arguments provided by the user into an array
which is passed to String.form. The contents of the String itself
is passed as format string. This allows a very compact syntax to
format any kind of object, array or number.
For details see String.form.
Defined in: String.js.
Author: Thomas Bretz.
Defined in: String.js.
Author: Thomas Bretz.
var result = "%5d = %12s".$(5, "five");
- Parameters:
- arg
- An argument to be formated.
- . . . Optional
- An undefined number of additional optional arguments.
- Throws:
- see String.form
- Returns:
- {String} see String.form
- See:
- String.form
{Interger}
count(regex, case)
Like String match, but return the number counts how often
the regular expression matches.
Defined in: String.js.
Author: Thomas Bretz.
Defined in: String.js.
Author: Thomas Bretz.
var result = "Thomas Bretz".count("[hme]"); // returns 3
- Parameters:
- {String} regex
- The regular expression to search for, e.g., "s" (to count the s's) or "As+A" (to count how often s's are surrounded by A's)
- {Boolean} case Optional, Default: false
- Search is case insensitive if set to true.
- Returns:
- {Interger} The number of occurances of the regular expression
<static>
{String}
String.form(format, elements)
Format a string (similar to printf in C).
This function replaces modifiers (very similar to printf) with
a formated version of the argument. Since JavaScript does implicit
format conversion already, this is actually a very powerful tool,
because the type of th arguments to be formated is of no importance.
Implicit conversion means that also arrays and objects can be given
as arguments. A shortcut is available as $-extension of the native
String class.c: %[[-][0]N]c
Extracts the first element from an array. In case of a String this
is the first character. s: %[[-][0]N]s
Converts the argument to a string using toString() f: %[[-][0]N[.n]]f
Converts to a Number value with n internal decimal places p: %[[-][0]N[.n]]p
Converts to a Number value with a precision of n decimal places e: %[[-][0]N]e
Converts to an exponential with a precision of n decimal places x: %[[-][0]N[#b]x
Converts to an integer value using the basis b for conversion
(default is 16 for hexadecimal) d: %[[-][0]N[.n][#b]]d
Converts from a value using the basis b for conversion (default
is 10 for decimal). The integer can be rounded to the given precision n.
Note that this function is completely written in JavaScript. It can be found in InterpreterV8.cc.
The following modifiers are available (optional parts are put in
brackets:
The output string will have at least a length of N. If 0 is given,
it is filled with 0's instead of white spaces. If prefixed by a minus
the contents will be left aligned.
Defined in: String.js.
var result; result = String.form("%5d %3d", [ 5, "2" ]); result = String.form("%5x", [ 12 ]); result = String.form("%#16d", [ "b" ]); result = String.form("%s", [ [ 1, 2, 3, ] ]); result = String.form("%s", [ { a:1, b:2, c:3, } ]); var abbrev = "%03d".$(42);
- Parameters:
- {String} format
- The format string defining how the given argument elements are formated
- {Array} elements
- An array with the elements to be formated
- Returns:
- {String} The formated string is returned.
- See:
- String.$