Javascript Strings: Difference between revisions
No edit summary |
No edit summary |
||
| Line 54: | Line 54: | ||
* <div id="match">'''match(reg)'''</div> | * <div id="match">'''match(reg)'''</div> | ||
Function that matches a string against a Regular Expression. | Function that matches a string against a [[http://en.wikipedia.org/wiki/Regular_expression|Regular Expression]]. | ||
Returns an Array holding the match results and some extra information: | |||
* [0] holds the whole matching string | |||
* [1]... hold the captured substrings | |||
* index holds the 0-based index of the first matching character | |||
* input holds the [[Javascript_RegExp|RegExp]] object used in match | |||
If the string does not match the regular expression ''reg'' then the function returns '''null'''. | |||
x="abc123"; | |||
m = x.match(/([0-9])/); | |||
Engine.output((typeof m) + " index=" + m.index + " input=" + m.input); | |||
'''object index=3 input=([0-9])''' | |||
* <div id="toLowerCase">'''toLowerCase()'''</div> | * <div id="toLowerCase">'''toLowerCase()'''</div> | ||
Function that converts a string to lower case. | Function that converts a string to lower case. | ||
Returns the string converted to lower case characters. | |||
x="AbCd"; | |||
Engine.output(x.toLowerCase()); | |||
'''abcd''' | |||
* <div id="toUpperCase">'''toUpperCase()'''</div> | * <div id="toUpperCase">'''toUpperCase()'''</div> | ||
Function that converts a string to upper case. | Function that converts a string to upper case. | ||
Returns the string converted to upper case characters. | |||
x="AbCd"; | |||
Engine.output(x.toUpperCase()); | |||
'''ABCD''' | |||
* <div id="trim">'''trim()'''</div> | * <div id="trim">'''trim()'''</div> | ||
Function that removes leading and trailing spaces from a string. | Function that removes leading and trailing spaces from a string. | ||
Returns the string with all initial and final whitespaces removed. | |||
x=" a bc d "; | |||
Engine.output(">" + x.trim() + "<"); | |||
'''>a bc d<''' | |||
* <div id="sqlEscape">'''sqlEscape()'''</div> | * <div id="sqlEscape">'''sqlEscape()'''</div> | ||
Function that performs SQL escaping on a string. | Function that performs SQL escaping on a string. | ||
Note that backslash escaping is an SQL extension | |||
x="a'b'c\\backslash"; | |||
Engine.output(x.sqlEscape()); | |||
'''<nowiki>a''b''c\\backslash</nowiki>''' | |||
* <div id="startsWith">'''startsWith(str)'''</div> | * <div id="startsWith">'''startsWith(str)'''</div> | ||
Revision as of 14:52, 11 September 2013
Although the String object is not directly supported by Yate's Javascript these methods are applicable to most non-object variables.
Note that these are not real properties of strings and cannot be enumerated!
- length
Property that describes the length of a string as a decimal number.
Returns numeric length of string
x="abcd"; Engine.output(x.length); 4
- charAt(pos)
Function that returns a character at a certain 0-based position in string.
Returns a string containing a single character or empty if pos is out of bounds.
x="abcd"; Engine.output(x.charAt(3)); d
- indexOf(str)
Function that returns the position of a substring in another string.
Returns 0-based position of str or -1 if str is not found
x="abcd";
Engine.output(x.indexOf("bc"));
1
- substr(offs,len)
Function that returns a substring.
Returns a string that starts at 0-based position offs and has at most len characters
x="abcd"; Engine.output(x.substr(1,2)); bc
- substr(offs)
Function that returns a substring.
Returns a string that starts at 0-based position offs to the end of the original string
x="abcd"; Engine.output(x.substr(2)); cd
- match(reg)
Function that matches a string against a [Expression].
Returns an Array holding the match results and some extra information:
- [0] holds the whole matching string
- [1]... hold the captured substrings
- index holds the 0-based index of the first matching character
- input holds the RegExp object used in match
If the string does not match the regular expression reg then the function returns null.
x="abc123"; m = x.match(/([0-9])/); Engine.output((typeof m) + " index=" + m.index + " input=" + m.input); object index=3 input=([0-9])
- toLowerCase()
Function that converts a string to lower case.
Returns the string converted to lower case characters.
x="AbCd"; Engine.output(x.toLowerCase()); abcd
- toUpperCase()
Function that converts a string to upper case.
Returns the string converted to upper case characters.
x="AbCd"; Engine.output(x.toUpperCase()); ABCD
- trim()
Function that removes leading and trailing spaces from a string.
Returns the string with all initial and final whitespaces removed.
x=" a bc d ";
Engine.output(">" + x.trim() + "<");
>a bc d<
- sqlEscape()
Function that performs SQL escaping on a string.
Note that backslash escaping is an SQL extension
x="a'b'c\\backslash"; Engine.output(x.sqlEscape()); a''b''c\\backslash
- startsWith(str)
Function that checks if a string starts with a specific substring.
- endsWith(str)
Function that checks if a string ends with a specific substring.
- split(sep)
Function that splits a string into an Array at a separator.
- toString()
Function that converts a number to string using numbering base 10.
- toString(base)
Function that converts a number to string using a specified numbering base.