JavaScript – Strings manipulation

Statements:

// single or double quote are allowed
var carname="Volvo XC60";
var carname='Volvo XC60';

// Quotes inside a string
var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';

var answer='It\'s alright';
var answer="He is called \"Johnny\"";               

Properties and Methods:


// ###################################################################
// Add HTML text tags
// ###################################################################

var str="Hello world!";
document.write(str.bold() + "<br>"); // the result is <b>Hello world!</b> 
document.write(str.blink() + "<br>") // the result is <blink>Hello world!</blink> Firefox support only
document.write(str.big() + "<br>"); // the result is <big>Hello world!</big> 
document.write(str.anchor() + "<br>"); // the result is <a name="undefined">Hello world!</a>
document.write(str.fontcolor() + "<br>"); // the result is <font color="undefined">Hello world!</font>
document.write(str.fontsize() + "<br>"); // the result is <font size="undefined">Hello world!</font>
document.write(str.italics() + "<br>"); // the result is <i>Hello world!</i>
document.write(str.small() + "<br>"); // the result is <small>Hello world!</small>
document.write(str.strike() + "<br>"); // the result is <strike>Hello world!</strike>
document.write(str.sub() + "<br>"); // the result is <sub>Hello world!</sub> - pedice
document.write(str.sup() + "<br>"); // the result is <sup>Hello world!</sup> - apice

// ###################################################################
// Character position - index of a character
// ###################################################################

// You can access each character in a string with its position
// The first index position is 0
var character=carname[0]; // the result is c
var character=carname[1]; // the result is a

var str = "HELLO WORLD";
var res = str.charAt(0)   // The result is H - the character at index 0

var str = "HELLO WORLD";
var n = str.charCodeAt(0) // The result is 72 - the unicode of character at index 0

var str="Hello World";
var n=str.indexOf("World");     // n stores 7 - the position of W from the first letter
var n=str.lastIndexOf("World"); // n stores 5 - the position of W from the last letter                             
//  n stores -1 if the specified text is not found

// Return position of the string you are searching
var str = "Visit Luce Digitale!";
var n = str.search("Luce"); // the result is 6 (the first index value is 0)

// ################################################################### 
// String Lenght
// ###################################################################

var txt = "Hello World!";
var x = txt.length;       // x stores 12

// ###################################################################
// Find a string in string
// ###################################################################

// Maching Content
var str="Hello world!";
document.write(str.match("world") + "<br>"); // the result is world
document.write(str.match("World") + "<br>"); // the result is null
document.write(str.match("world!"));         // the result is world!

// Compare two strings in the current locale:
var str1 = "ab";
var str2 = "cd";
var n = str1.localeCompare(str2); // the result is -1, str1 is sorted before str2

// ###################################################################
// Manipulate Content
// ###################################################################

// Replace Content
str="Please visit Microsoft!"
var n=str.replace("Microsoft","Luce Digitale"); // the result is - Please visit Luce Digitale! -

// Join two strings
var str1 = "Hello ";
var str2 = "world!";
var res = str1.concat(str2); // the result is Hello world!

// Convert a Unicode number into a character
var res = String.fromCharCode(65); // the result is A

// Slice
var str = "Hello world!";
var res = str.slice(1,5); // the result is - ello -

// Split -> Great to write databases in a flat txt file!
var str = "How are you doing today?";
var res = str.split(" "); // the result is - How,are,you,doing,today? -

// Extract a part of a string
var str = "Hello world!";
var res = str.substr(1,4) // the result is - ello -

// Remove whitespace from both sides of a string
var str = " Hello World! "; // The value is - Hello World! -
alert(str.trim());          // The value is -Hello World!-

// ###################################################################
// Upper Case - Lower Case
// ###################################################################
var txt="Hello World!";       // String
var txt1=txt.toUpperCase();   // result is HELLO WORLD!
var txt2=txt.toLowerCase();   // result is hello world!

// ###################################################################
//  Convert a number to a string
// ###################################################################
var num = 15;
var n = num.toString(); // the result is 15, the value turs into a string now!

// ###################################################################
//  Convert a string to a number
// ###################################################################
var a = "1.45kg";
var n = parseFloat(a)  // the result is 1.45, the value turns into a number now!

// ###################################################################
// Convert a String to an Array - removing the commas, spaces, pipe 
// ###################################################################
var str="a,b,c,d,e,f";
var n=str.split(",");
alert(n[1]);                  // result is b! (the array index start at 0)

// Complete List
txt.split(",");   // Split on commas -> Great to read databases from a flat txt file!
txt.split(" ");   // Split on spaces
txt.split("|");   // Split on pipe 

Tips: to Write “databases” in flat .txt file you can use:

// Remove commas
var str="a,b,c,d,e,f";
var n=str.split(",");
alert(n[1]); // result is b! (the array index start at 0)

// Insert commas                 
// Split 
var str = "How are you doing today?";
var res = str.split(" "); // the result is - How,are,you,doing,today? -

Special Character in strings manipulation:

\'	single quote
\"	double quote
\\	backslash
\n	new line
\r	carriage return
\t	tab
\b	backspace
\f	form feed

My official WebSite >