String

The JavaScript String object sports a set of built-in methods and properties for making common string programming tasks easier for script authors when they work with string data.

var str1 = "Programming is fun."; var str2 = String("Programming is fun."); var str3 = new String("Programming is fun.");

All three of the approaches above will establish a string in JavaScript. Use either double quotes(“my string”) or single quotes(‘my string’) to wrap your string literal values. When you loosely establish strings(as in str1 above) without explicitly defining the data type they are treated as string objects due to JavaScript’s automatic data type casting feature.

Properties

length – returns the number of characters in a string. document.write("I like cake".length); var str = "I like cake"; document.write(str.length);

Methods

charAt – returns a character at a specified index in a string. var str = "Web Development"; var char1 = str.charAt(0); // W
var char2 = str.charAt(1); // e
var lastChar = str.charAt(str.length - 1); // 

 

 

charCodeAt – returns a character code at a specified index in a string. var str = "Web Development"; var charCode1 = str.charCodeAt(0); // "87"
var charCode2 = str.charCodeAt(1); // "101"
var lastCharCode = str.charCodeAt(str.length - 1); // "116"

 

 

 

concat– string concatenation to combine one or more strings. var str1 = "Programming"; var str2 = " is fun"; var str3 = " and refreshing."; var result = str1.concat(str2,str3);

 

 

 

fromCharCode – returns the string character from any unicode character identifier. var unicode = 87; var char = String.fromCharCode(unicode); // "W"

 

 

 

indexOf – returns the index position where a substring is first found in a string. var str = "Adam is nerdy and Susan is not."; var pos = str.indexOf("is"); //

 

 

 

5 lastIndexOf – returns the position where a substring is last found in a string. var str = "Adam is nerdy and Susan is not."; var pos = str.lastIndexOf("is"); //

 

 

 

 

24 match – return an array containing all matches of a regular expression based match. var str = "Hi friend, you must be my friend from friendland."; var regExp = /friend/gi; var result = str.match(regExp); TIP: “gi” in the regular expression syntax is to specify that we want a “global” and “incase-sensitive” match to occur. You can omit those or just use one or the other to make the match run a tad bit differently.

 

 

replace – replace string data using a regular expression. var str = "Hi friend, you must be my friend from friendland."; var regExp = /friend/gi; var result = str.replace(regExp, "enemy"); document.write(result); TIP: “gi” specifies that we want a “global” and “incase-sensitive” replace to occur.

 

 

search – search string for a match using a regular expression. var str = "You look like poop today."; var regExp = /poop/; if(str.search(regExp) > -1) { document.write("poop was found"); } else { document.write("no poop found"); } TIP: search will return “-1” if no match is found, and returns the index position number of the first match if a match is found.

 

 

split – split a string into an array of substrings by specifying a delimiter to split by. var str = "I love pie. I love cake."; var array1 = str.split("",11); // I, ,l,o,v,e, ,p,i,e,.
var array2 = str.split(" "); // I,love,pie.,I,love,cake.
var array3 = str.split("love"); // I , pie. I , cake.
TIP: split can be given an optional second parameter for limiting how many items get placed into the resulting array. I used the optional limiter when creating the array1 variable above.

 

 

slice – return a portion of a string by targeting the index positions of characters. var str = "My dog is cute."; var slice1 = str.slice(0,2); // "My"
var slice2 = str.slice(str.length - 5,str.length - 1); // "cute"

 

 

substr – returns a substring of a specified length starting from a specified index position. var str = "Welcome to the show!"; var sbstr1 = str.substr(0,7); // Welcome
var sbstr2 = str.substr(2,11); // lcome to th
var sbstr3 = str.substr(5); // me to the show!
TIP: Giving substr only one parameter will specify that you want the selection to go through to the end of the string.

 

 

 

substring – returns a substring from one index, up to another index but not including it. var str = "Big Wheels"; var substring1 = str.substring(0,5); // Big W
var substring2 = str.substring(5,str.length - 1); // heel
var substring3 = str.substring(2); // g Wheels
TIP: Giving substring only one parameter will specify that you want the selection to go through to the end of the string.

 

 

toLowerCase – returns an all lowercase version of a string. var str = "Here Lies Text."; var newStr = str.toLowerCase(); // here lies text.

 

 

toUpperCase – returns an all uppercase version of a string. var str = "Here Lies Text."; var newStr = str.toUpperCase(); // HERE LIES TEXT.

Author Extensions

The following custom extensions are prototypes courtesy of the author. They perform logic not built into the String object interface. We can create prototype based extensions and build them directly into objects in a global scope.

ucwords() – Uppercase the first character of each word in a string. String.prototype.ucwords = function(){ return this.replace(/\w\S*/g, function(s){ return s.charAt(0).toUpperCase() + s.substr(1).toLowerCase();} ); } var str = "hOw arE YOU toDAy?"; document.write( str.ucwords() );

renewdev

I Am Developer And Want To Share My Knowledge With The World..:) ;) . I Have Another Social Networking Website (Renewchat) And For Designing Or Developing Of Website Purpose You Can Contact Me On Renew Coder .. :) ;)

Leave a Reply

Your email address will not be published. Required fields are marked *