These 7 basic string functions are easy to implement adding them to the String object, are very useful and the resulting code is a lot more legible, maintainable and elegant.
1 y 2: Encode y Decode Uri
I use these functions a lot and very often forgot to add the suffix “Component” to them. It’s kind of unnatural. With these simple extensions, my problems just went away.
[javascript]
/**
* Encodes the string to URI format
* Usage:
* var str = “This is a string”;
* str = str.encodeUri();
* console.log( str );
* // Outputs “This%20is%20a%20string”
*/
String.prototype.encodeUri = function () {
return encodeURIComponent( this );
}; // end encodeUri
/**
* Decodes the string from Uri format
* Usage:
* var str = “This%20is%20a%20string”;
* str = str.decodeUri();
* console.log( str );
* // Outputs “This is a string”;
*/
String.prototype.decodeUri = function () {
return decodeURIComponent( this );
}; // end decodeUri
[/javascript]
3 y 4: EscapeRegEx & ReplaceAll
For some strange reason, when replacing one string for another only the first match is replaced. This is a problem, but easily solved with these two functions and adding these extensions, use is much simpler.
[javascript]
/**
* Creates a regular expression string from the string,
* Prepending “\” special characters “escaping” the chain
* Usage:
* var str = “Hi!”;
* str = str.escapeRegExp();
* console.log( str );
* // Outputs: “Hi\!”
*/
String.prototype.escapeRegExp = function () {
return this.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, “\\$1”);
}; // end function escapeRegExp
/**
* Replace all instances of “find” by “replace”
* in the string
* Usage:
* str = “Hello my name is $nombre, $nombre is my name”;
* str = str.replaceAll( “$nombre”, “Luis” );
* console.log( str );
* // Outputs: “Hello my name is Luis, Luis is my name”
*/
String.prototype.replaceAll = function ( find, replace ) {
return this.replace(new RegExp(find.escapeRegExp(), ‘g’), replace);
}; // end function replaceAll
[/javascript]
5, 6 y 7: Right, Left & Contains
If VB was your first language you surely miss these functions. To implement for all strings is quite simple, saves keystrokes and the result is very intuitive:
[javascript]
/**
* Returns “n” charactes to the left
* Usage:
* var str = “ABCDE”;
* str = str.left(3);
* console.log( str );
* // Outputs: “ABC”
*/
String.prototype.left = function ( n ){
if (n <= 0)
return "";
else if (n > this.length)
return this;
else
return this.substring(0,n);
}; // end function left
/**
* Returns “n” characteres to the right
* Uso:
* var str = “ABCDE”;
* str = str.right(3);
* console.log( str );
* // Outputs: “CDE”
*/
String.prototype.right = function ( n ) {
if (n <= 0)
return "";
else if (n > this.length)
return this;
else {
var iLen = this.length;
return this.substring(iLen, iLen – n);
}
}; // end function right
/**
* Returns true if the string contains the parameter “str”
* Usage:
* var str = “Hello world!”;
* var lookFor = “world”;
* var inString = str.contrains(lookFor);
* console.log(inString);
* // Outputs: true
*/
String.prototype.contains = function ( str ) {
return (
this.indexOf( str ) > -1
);
}; // end function contains
[/javascript]
Running this script at the beginning of your code will allow you to use these extensions, will make your life easier and allows you to have a clean and elegant code. Happy Coding! 🙂
Gordan, a hematologist oncologist, replaces Dr buy generic cialis
Maca has been used as a food source in the Andes and has been traditionally used to support stamina, hormonal health, energy and libido cialis from usa pharmacy
cialis professional Nexplanon and Mirena are contraindicated in known or suspected pregnancy
Rivollier A, He J, Kole A, Valatas V, Kelsall BL cialis generic It is very important that house owners who monitor blood glucose readings at home do NOT change their cat s insulin dose without first consulting with their veterinarian Close monitoring by both the proprietor and the veterinarian is an essential part of treatment for a NM Crisis Line does bread help low blood sugar diabetic cat Regular monitoring will help determine the best insulin dose for every cat in addition to help avoid issues, such as hypoglycemia or uncontrolled diabetes and diabetic ketoacidosis Regular assessments of weight, water intake, and urge for food should be recorded to assist determine if remedy goals are being met Diabetes mellitus is a situation in which the body cannot correctly produce or respond to the hormone insulin
buying cialis online safely Do not coadminister with aliskiren in patients with diabetes mellitus or with renal impairment ie, GFR Patient Education perindopril erbumine oral
After trying to conceive for a year with no success we began to explore options with my doctor, which was mainly to try clomid to regulate my cycles how to buy cialis 6 Loracarbef success rate 19 less than control S
cialis online We made no restrictions on the language and time of studies
Progesterone stimulates pancreatic cell proliferation in vivo generic cialis from india 8, 9, 10 Schizophrenia occurs less frequently and has a later average age of onset in women
Lana Butner, ND, LAc, is a board certified naturopathic doctor and licensed acupuncturist in private practice in New York City cialis 10mg 00591 0343 Verapamil Hydrochloride Actavis Pharma, Inc
1, 2 While rarely used, the monoamine oxidase inhibitor MAOI selegiline may also cause false positive amphetamine results due to its l amphetamine and l methamphetamine metabolites azithral 500 buy online serpina el paxil cr engorda The barred owls have been encroaching on the territory of the northern spotted owl for nearly 40 years, and according to State Supervisor of the Oregon Fish and Wildlife Office Paul Henson, the two species do not live well together
generic cialis from india 1 Triamterene was found to cross the placental barrier and appear in the cord blood of animals
The DNA binding activities analyzed using EMSA were confirmed by the ChIP assay Fig generic cialis tadalafil drafted manuscript; F
Third, the up regulation of the Mafb expression started at stage VII is a dramatic response to the RA signaling that is periodically activated during stages VII XII, and this activation alters the expression of various Sertoli cell s genes 13, 30 cialis buy online