replace

7 functions extendind Strings in Javascript that will save you a lot of code and are very elegant

Posted by admin on March 29, 2016
javascript / 13 Comments

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! 🙂

Tags: , , , , , , , ,