Javascript Strings

From Yate Documentation
(Difference between revisions)
Jump to: navigation, search
 
Line 54: Line 54:
  
 
* <div id="match">'''match(reg)'''</div>
 
* <div id="match">'''match(reg)'''</div>
Function that matches a string against a [[http://en.wikipedia.org/wiki/Regular_expression|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:
 
Returns an Array holding the match results and some extra information:
Line 112: Line 112:
 
* <div id="startsWith">'''startsWith(str)'''</div>
 
* <div id="startsWith">'''startsWith(str)'''</div>
 
Function that checks if a string starts with a specific substring.
 
Function that checks if a string starts with a specific substring.
 +
 +
Returns true if ''str'' is the prefix of the string.
 +
 +
x="abcd";
 +
Engine.output(x.startsWith("ab"));
 +
 +
'''true'''
  
 
* <div id="endsWith">'''endsWith(str)'''</div>
 
* <div id="endsWith">'''endsWith(str)'''</div>
 
Function that checks if a string ends with a specific substring.
 
Function that checks if a string ends with a specific substring.
 +
 +
Returns true if ''str'' is the suffic of the string.
 +
 +
x="abcd";
 +
Engine.output(x.endsWith("bcd"));
 +
 +
'''true'''
  
 
* <div id="split">'''split(sep)'''</div>
 
* <div id="split">'''split(sep)'''</div>
 
Function that splits a string into an Array at a separator.
 
Function that splits a string into an Array at a separator.
 +
 +
Returns an Array object holding the string fragments.
 +
 +
x="abc,def,xyz";
 +
a=x.split(",");
 +
Engine.output("[0]=" + a[0] + " [1]=" + a[1] + " [2]=" + a[2]);
 +
 +
'''[0]=abc [1]=def [2]=xyz'''
  
 
* <div id="toString">'''toString()'''</div>
 
* <div id="toString">'''toString()'''</div>
 
Function that converts a number to string using numbering base 10.
 
Function that converts a number to string using numbering base 10.
 +
 +
x=1234;
 +
s=x.toString();
 +
Engine.output((typeof s) + " " + s)
 +
 +
'''string 1234'''
  
 
* '''toString(base)'''
 
* '''toString(base)'''
 
Function that converts a number to string using a specified numbering base.
 
Function that converts a number to string using a specified numbering base.
 +
 +
x=1234;
 +
s=x.toString(16);
 +
Engine.output((typeof s) + " " + s)
 +
 +
'''string 4d2'''

Latest revision as of 17:01, 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 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 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.

Returns true if str is the prefix of the string.

x="abcd";
Engine.output(x.startsWith("ab"));

true
  • endsWith(str)

Function that checks if a string ends with a specific substring.

Returns true if str is the suffic of the string.

x="abcd";
Engine.output(x.endsWith("bcd"));

true
  • split(sep)

Function that splits a string into an Array at a separator.

Returns an Array object holding the string fragments.

x="abc,def,xyz";
a=x.split(",");
Engine.output("[0]=" + a[0] + " [1]=" + a[1] + " [2]=" + a[2]);

[0]=abc [1]=def [2]=xyz
  • toString()

Function that converts a number to string using numbering base 10.

x=1234;
s=x.toString();
Engine.output((typeof s) + " " + s)

string 1234
  • toString(base)

Function that converts a number to string using a specified numbering base.

x=1234;
s=x.toString(16);
Engine.output((typeof s) + " " + s)

string 4d2
Personal tools
Namespaces

Variants
Actions
Preface
Configuration
Administrators
Developers