In the programming world strings mean text, not the ordinary strings we use to sew. In JS, strings are used for storing and manipulating text. Strings represented by quotes, either double or single in JS. But in other programming languages you’ll see both cannot be used for the similar purpose with the concept of character. But in JS it can.
var dog = “Timmy”;
var dog = ‘Timmy’;
You can use quotes inside a string, as long as they don’t match the quotes surrounding the string. For example look at below.
var result = “My dog is called ‘Timmy'”;
var result = ‘My dog is called “Timmy”‘;
var result = ‘My dog’s name is “Timmy”‘; //Here you’ll face with a problem. Not properly quoted the text
To make the last one a proper text string, you must use escape character backslash () .
var result = ‘My dog’s name is “Timmy”‘;
The escape character () can be used to insert apostrophes, new lines, quotes, and other special characters into a string. There are several other kink of escape characters you can use inside of the string for different purposes. Backslash is used so it will not get jumbled with the default usage of the character.
Strings can also be defined as objects with the keyword new.
var firstName = new String(“Timmy”)
But don’t create String objects. They slow down execution speed, and produce nasty side effects.[code language=”js”] var x = “Timmy”; // x is a string
var y = new String(“Timmy”); // y is an object
document.getElementById(“demo”).innerHTML = x===y;
You’ll see that two things are not equal.
String Properties and Methods
With the length property of the string, you can count how many characters that text contain.
var txt= “My dog is called ‘Timmy'”;
var count= txt.length;
Variable count will have value 17.
Finding a String in a String
It returns the index of (the position of) the first occurrence of a specified text in a string:
var txt= “My dog is called ‘Timmy’ and Timmy is a good doggy”;
var pos = txt.indexOf(“Timmy”);
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string
var pos = txt.lastIndexOf(“Timmy”);
Extracting String Parts
There are 3 methods for extracting a part of a string:
- slice(start, end)
- substring(start, end)
- substr(start, length)
Replacing String Content
Replaces a specified value with another value in a string
Converting to Upper and Lower Case
Joins two or more strings even though it can be done with plus operator.
Extracting String Characters
There are 2 safe methods for extracting string characters:
var str = “HELLO WORLD”;
str.charAt(0); // returns H
str.charCodeAt(0); // returns 72
We are saying safe here because, the unsafe way to extract string is using as an array before convert it to an array.
Converting a String to an Array
A string can be converted to an array with the split() method using a specific delimiter/separator. If the separator is omitted, the returned array will contain the whole string in index . If the separator is “”, the returned array will be an array of single characters.
var txt = “any,by,cat,dog,eat”; // String
txt.split(“,”); // Split on commas
txt.split(“”); // Split in characters