JavaScript String Methods, Properties, and Objects
A javascript string is data type in any programming language and it’s used to store text rather than numbers. In Javascript, strings are used for manipulating the text. A string can be declared with enclosing of single quotes or double quotes or backticks like below examples.
var singleQuote = 'single-quote';
var doubleQuote = "double-quote";
var backTicks = back-ticks
;
You can use quotes inside the string, as long as they didn’t match with surrounding quotes.
var person = "It's me..";
var company = 'This is called "Cronj" ';
var company = "This is called 'Cronj’ ";
Javascript Strings have to place within the quotes, otherwise, it will misunderstand:
var country = "This is "India” country'';
In the above example, the string will take up to “This is “. So, The solution here is to use backslash(\). Backslash character converts special characters into the string character.
Eg: Double Quote (\’)
var country = "This is \"India\" country";
Output: This is “India” country
Eg: Single Quote (\”)
var country = 'This is \'India\' country';
Output: This is ‘India’ country
Eg: Backslash (\)
var specialCharacter = "This character \\ is called backslash";
Output: This character \ is called backslash.
Find String Length:
In Javascript, we have a built-in property called length. By using length property we can find the length of a javascript string.
var alphabates = "Cronj";
var strLength = alphabates.length;
Long code lines break:
Programmers are used to avoiding code lines above 80 characters. In Javascript best practice is to break line after an operator
Eg: document.getElementById(“string”).innerHTML = “Hello world”;
We can also break the code within the text also, like below by using the backslash.
Eg: document.getElementById(“string”).innerHTML = “Hello \
World”;
Some browsers do not allow spaces after \. So safer way to break like using javascript string concatenation using the ‘+‘ operator.
Eg: document.getElementById(“string”).innerHTML = “Hello” +
“World”;
Sting Objects:
In Javascript, String can be objects. Here strings are also be defined as objects with the keyword new:
var personNameString = "John";
var personNameObject = new String("John");
Here typeof(personNameString) will return string and typeof(personNameObject) will return object. But the better way to define string without new, because it will affect execution speed and it causes unexpected results.
String properties:
Constructor returns the string’s constructor function. Length returns the length of a javascript string. Prototype allows you to add methods and properties to an object.
String Methods:
charAt():
charAt() method will return the character at the specified position(index).
Eg:
var exString = "Hello world";
var response = exString.charAt(6);
console.log(response);
Output:
charCodeAt():
charCodeAt() method will return the Unicode of the character at the specified index in a javascript string.
Eg:
var exString = "Hello world";
var response = exString.charAt(6);
console.log(response);
Output: 72
concat():
concat() method is used to join strings. This method doesn’t change the existing string, It will return the new concatenated string as output.
Eg:
var string1 = "Hello";
var string2 = "world";
var response = string1.concat(string2);
console.log(response);
Output: Helloworld
You can concatenate two or more javascript strings.
Eg:
var string1 = "Hello";
var string2 = " world";
var string3 = " Have a nice day!!";
var response = string.concat(string2, string3); console.log(response);
Output: Hello world Have a nice day!!
[Read: Node Js: Non-blocking or asynchronous | Blocking or synchronous]
endsWith():
endsWith() method determines whether a javascript string ends with specified string or not. This method will return true if a string exists or it will return false if the string does not exist.
Eg:
var string1 = "Hello world, Have a nice day!";
var a = string1.endsWith("day!");
console.log(a);
Output: true
fromCharCode():
fromCharCode() method converts the unicode character.
Eg:
var string1 = String.fromCharCode(67);
console.log(string1);
Output: C
includes():
includes() method determines whether a string contains in the characters of the specified javascript string, and returns true if the string contains the characters and false if not contains.
var string1 = "Hello world, Have a nice day!";
var a = string1.includes("Have");
Output: true
indexOf():
indexOf() method determines the first occurrence of a specified value in a javascript string and it returns -1 if the value is not found in our occurrence.
var string1 = "Hello world, Have a nice day!";
var a = string1.indexOf("Good");
Output: -1
lastIndexOf():
lastIndexOf() method returns the position of the last occurrence of a specified value in a javascript string and it returns -1 if the value is not found in our occurrence.
Eg:
var string1 = "Hello world, Have a nice day!";
var a = string1.lastIndexOf(" day!");
Output: 24
match():
match() method will search a string for a match against a regular expression and returns the matches as an array.
Eg:
var string = "The rain in India stays mainly in the plain";
var response = string.match(/ain/g);
console.log(response);
Output: ain,ain,ain
repeat():
repeat() method always returns a new string with a specified number of copies of the string.
Eg:
var string = "Winter season! ";
var response = string.repeat(2);
console.log(response);
Output: Winter season! Winter season!
replace():
replace() will search a javascript string for a specified value or a regular expression and returns a new string where the specified values are replaced.
Eg:
var string = "Winter season!";
var response = string.replace("!", "@");
console.log(response);
Output: Winter season@
search():
search() method searches a javascript string for a specified value or a regular expression and returns the position of the match.
Eg:
var string = "Winter season!";
var response = string.search("season");
console.log(response);
Output: 7
slice():
slice() method to extract a part of the javascript string and it returns a new string. It takes two parameters like start and endpoints that what we want to extract.
Eg:
var string = "Winter season!";
var response = string.slice(0, 6);
console.log(response);
Output: Winter
split():
split() method is used to split a javascript string into an array of substrings and it returns a new array
Eg:
var string = "Winter season!";
var response = string.split(" ");
console.log(response);
Output: Winter, season!
[Read: Node JS WebSocket: Examples | Chat Features | Client-Server communication]
substr():
substr() method always extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
Eg:
var string = "Winter season!";
var response = string.substr(0, 8);
console.log(response);
Output: Winter s
substring():
substring() method will extract the characters from the two specified positions in parameters and returns the new substring. Two parameters are starting position and ending positions.
Eg:
var string = "Winter season!";
var response = string.substring(0, 8);
console.log(response);
Output: Winter s
toString():
toString() method returns the value of a string. If the value in the variable is object then it will convert as a string object.
Eg:
var string = "Winter season!";
var response = string.toString();
console.log(response);
Output: Winter season!
trim():
trim() method removes the white spaces of a sting in both(starting and ending) sides.
Eg:
var string = " Winter season! ";
var response = string.trim();
console.log(response);
Output: Winter season!