Skip to content

Instantly share code, notes, and snippets.

@cultmind
Last active November 1, 2021 16:33
Show Gist options
  • Save cultmind/de1d7360a6dd4421a84a53e0d56d6099 to your computer and use it in GitHub Desktop.
Save cultmind/de1d7360a6dd4421a84a53e0d56d6099 to your computer and use it in GitHub Desktop.
string : String is a primitive data type in JavaScript. A string is textual content.
It must be enclosed in single or double quotation marks.
Since, string is character index, it can be accessed using for loop and for-of loop.
( A JavaScript string is zero or more characters written inside quotes. )
Example
var name = "Ganesh";
Result :
"Ganesh"
String Length : To find the length of a string, use the built-in length property
Example
var numbers = "123456789";
number.length;
Result :
9
Strings Can be Objects :
Normally, JavaScript strings are primitive values, created from literals:
let a = "Ganesh";
But strings can also be defined as objects with the keyword new:
let b = new String("Ganesh");
Result :
typeof a
'String'
typeof b
'object'
JavaScript methods for searching strings:
1.String.indexOf()
2.String.lastIndexOf()
3.String.startsWith()
4.String.endsWith()
String.indexOf() : The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string.
Note : JavaScript counts positions from zero.
0 is the first position in a string, 1 is the second, 2 is the third ...
Example
var str = "HELLO EVERY ONE";
str.indexOf("EVERY");
Result :
6
String.lastIndexOf() : The lastIndexOf() method returns the index of the last occurrence of a specified text in a string.
Example
let str = "HELLO EVERY ONE";
str.lastIndexOf("ONE");
Result :
12
String.search() : The search() method searches a string for a specified value and returns the position of the match:
Example
let example = "I LOVE WEB SERIES";
example.search("SERIES"):
Result :
11
Extracting String Parts
There are 3 methods for extracting a part of a string:
1.slice(start, end)
2.substring(start, end)
3.substr(start, length)
slice() : slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: the start position, and the end position (end not included).
Note : JavaScript counts positions from zero. First position is 0 .
Example
let names = "Ganesh, Mahesh, Suresh";
names.slice(8, 14);
Result :
Mahesh
If a parameter is negative, the position is counted from the end of the string.
Example
let x = "Ganesh, Mahesh, Suresh";
x.slice(-22, -8);
Result :
'Ganesh, Mahesh'
substring() : The difference is that substring() cannot accept negative indexes.
substring() is similar to slice().
Example
let str = "Apple, Banana, Kiwi";
str.substr(0,13);
Result :
'Apple, Banana'
replace() : The replace() method replaces a specified value with another value in a string.
Example 1 :
let text = "Please visit Microsoft!";
text.replace("Microsoft", "W3Schools");
Result :
'Please visit W3Schools!'
To replace case insensitive, use a regular expression with an /i flag (insensitive):
Example 2 :
let text = "Please visit Microsoft!";
text.replace(/MICROSOFT/i, "W3Schools");
Result :
'Please visit W3Schools!'
Note : that regular expressions are written without quotes.
To replace all matches, use a regular expression with a /g flag (global match):
Example 3 :
let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace(/Microsoft/g, "W3Schools");
Result :
'Please visit W3Schools and W3Schools!'
toUpperCase() : A string is converted to upper case with toUpperCase()
Example
let text1 = "Hello , Hii!";
let text2 = text1.toUpperCase();
Result :
'HELLO , HII!'
toLowerCase() : A string is converted to lower case with toLowerCase()
Example
let text1 = "Hello World!";
text1.toLowerCase();
Result :
'hello world!'
concat() : concat() joins two or more strings:
Example 1 :
let text1 = "Hello";
let text2 = "World";
text1.concat(" ", text2);
Result :
'Hello World'
The concat() method can be used instead of the plus operator. These two lines do the same:
Example 2 :
text = "Hello" + " " + "World!";
text = "Hello".concat(" ", "World!");
Result :
'Hello World!'
NOTE : All string methods return a new string. They don't modify the original string.
Formally said: Strings are immutable: Strings cannot be changed, only replaced.
trim() : The trim() method removes whitespace from both sides of a string.
Example
let text = " Hello World! ";
text.trim()
Result :
'Hello World!'
charAt() : The charAt() method returns the character at a specified index (position) in a string.
Example
let text = "CULTMIND";
text.charAt(1)
Result :
'U'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment