« Back to the index

WA.String.* examples

The String object has been enhanced with 4 extra methods:
sprintf: A template replacement method.
escape: A method to escape special characters with a \: ', " and \ will turn into: \', \" and \\
padding: A method to fill the beginning of the string with a single character up to have the desired size.
trim: A method to remove left and right spaces of a string.

WA.String.sprintf.call(anystr, str, params); WA.String.escape.call(anystr, str); WA.String.padding.call(anystr, repeat, motif, str); WA.String.trim.call(anystr);
The 4 methods may also be used directly applied to the instance:
'the string with elements {0} {1}'.sprintf(params); 'the string " to \ escape'.escape(); 'the string to fill'.padding(motif, repeat); ' the string to trim '.trim();

String.sprintf

Let's use a basic function that makes a template:
function test1() { var template = '<div id="{0}" style="border: 1px solid red; padding: 5px; margin: 2px;">Name: {1}<br />Price: $ {2} USD<br /></div>'; var data = [ {"id":'prod1',"name":'Video of the Space',"price":132.30}, {"id":'prod2',"name":'Book of the Sea',"price":99.90}, {"id":'prod3',"name":'Music of the Silence',"price":5.99} ]; for (var i=0, l=data.length; i<l; i++) WA.get('#test1').append(template.sprintf(data[i].id, data[i].name, data[i].price)); }
Show the result
You may also use directly the content of the objects to replace into the string with the respective IDs of the elements of the object into the template:
function test2() { var template = '<div id="{id}" style="border: 1px solid red; padding: 5px; margin: 2px;">Name: {name}<br />Price: $ {price} USD<br /></div>'; var data = [ {"id":'prod1',"name":'Video of the Space',"price":132.30}, {"id":'prod2',"name":'Book of the Sea',"price":99.90}, {"id":'prod3',"name":'Music of the Silence',"price":5.99} ]; for (var i=0, l=data.length; i<l; i++) WA.get('#test1').append(template.sprintf(data[i])); }
Show the result

String.escape

Let's escape some strings:
Node the parenthesis ( and ) around the second string, to build first the string, and THEN escape it all.
function test3() { var escaped1 = String.escape('This is the '+"'first'"+ ' string to "escape"' + ', windows dir: C:\windows32'); var escaped2 = ('This is the '+"'second'"+ ' string to "escape"' + ', windows dir: C:\applications').escape(); WA.get('#test3').text(escaped1 + '\n' + escaped2); }
Show the result

String.padding

Let's append some zeros and - before some numbers and variables:
If there is no motif character, spaces will be appended at the beginning.
This method is very usefull to fill numbers with 0, for instance for a month or a day in a 2 digits presentation.
function test4() { var line1 = String.padding(10, '0', '123'); var line2 = 'abc'.padding(15, '-'); var line3 = '1'.padding(5); WA.get('#test4').text(line1 + '\n' + line2 + '\n' + line3); }
Show the result

String.trim

Let's trim some strings:
Note that the &nbsp; html forced space will ALSO be trimed.
function test5() { var trimed1 = String.trim(' A string to trim '); var trimed2 = ' &nbsp; Another string to trim '.trim(); WA.get('#test5').text('==' + trimed1 + '==\n==' + trimed2 + '=='); }
Show the result


« Back to the index