JavaScript String
1. String.
Concept that cover in this post about String
i. String is a primitive Data type in Javascript.
ii. A string can be any text inside double or single quotes.
iii. Check String length
4. JavaScript Sting Slice
5. String replace
6. To replace case insensitive, use a regular expression with an /i
7. Check string data type
8. string concatenate
9. Escape Character \"char\" or \'character\'
10. JavaScript String as Object
11. Comparing two JavaScript objects always returns false.
12. Check if a string endsWith "family"
13. include()
14. indexOf()
15. lastIndexOf ()
16. match()
17. String prototype
17. String repeat()
18. String search();
19. String split()
20. String startsWith()
21. substr()
22. toLowerCase() and toUpperCase()
23. trim() method
i. String is a primitive Data type in Javascript.
ii. A string can be any text inside double or single quotes.
var str1 = "This is string inside Duble Quotes";
console.log(str1);
var str1 = 'This is string inside single Quotes';
console.log(str1);
output:
This is string inside Double Quotes
This is string inside single Quotes
iii. Check String length
var str1 = 'This is string inside single Quotes';
console.log(str1.length);
output: 36
4. JavaScript Sting Slice
// This is string slice
let str = "Apple, Banana,Graps, Blackberry";
let part = str.slice(7,13);
console.log(part);
output:
// This is string slice
let str = "Apple, Banana,Graps, Blackberry";
let part = str.slice(7,19);
console.log(part);
// This is string slice
let str = "Apple, Banana,Graps, Blackberry";
let part = str.slice(5);
console.log(part);
output: , Banana,Graps, Blackberry
var parents = ("parents is 55 and 50 years old.");
// var age = parents.slice(11, 20);
var age = parents.slice(-20,-11);
console.log(age);
If a parameter is negative, the position is counted from the
end of the string.
// This is string slice
let str = "Apple, Banana,Graps, Blackberry";
let part = str.slice(-10);
console.log(part);
// This is string slice
let str = "Apple, Banana,Graps, Blackberry";
let part = str.slice(7,19);
console.log(part);
5. String replace
The replace() method replaces only the first match.
the replace() method is case sensitive
// This is string Replacing
let str = "Apple, Banana,Graps, Blackberry";
let part = str.replace("Banana", "Mango");
console.log(part);
output: Apple, Mango,Graps, Blackberry
var life = ("what kind of life i am living");
var hell = life.replace( "life","fucking life,");
console.log(hell);
output: what kind of fucking life, i am living
6. To replace case insensitive, use a regular expression with an /i
let text = "Please visit London!";
let newText = text.replace(/LONDON/i, "NewYork");
console.log(newText);
7. Check string data type using "typeof" function.
Number data type
var dad = 10000;
console.log(typeof dad);
output: number
String data type
let dad = "Dad earn only Rs 10000 once a month.";
console.log(typeof dad);
output: String
Boolean data
const mom = true;
console.log("mom is living a hard life",mom);
console.log(typeof mom);
output:
mom is living a hard life true
boolean
8. string concatenate
var srt = "Dont waste time ";
var srt2 = "This is the time get backup";
console.log(srt.concat(srt2))
output: Dont waste time This is the time get backup
// let string concatenate
var mom = "mom is 50+ years old ";
var dad = "dad is 55+ years old";
var con = mom + dad;
console.log(con);
output: mom is 50+ years old dad is 55+ years old
var dad = "dad still working as a concrete labour";
dad += " and I am not still helping my parents why?.";
console.log(dad);
output: dad still working as a concreate labour and I am not still helping my parents why
9. Escape Character \"char\" or \'character\'
This is Escape Character.
var system = "life is hard \'noOne\' know how u are living.";
console.log(system);
output: life is hard 'noOne' know how u are living.
\n Next line function
var alertt = ("Not want to go home \n in the evening pocket is zero, no customer came.");
console.log(alertt);
output: Not want to go home
in the evening wallet is zero, no customer came.
\r carriage Return
var cafe = "I have a cybercafe with \r one computer and a printer.";
console.log(cafe);
output:
I have a cybercafe with
one computer and a printer.
\t Horizontal Tabulator
var sysalert = "Life is hard move \t otherwise die";
console.log(sysalert);
output: Life is hard move otherwise die
\v Vertical Tabulator CHECK ON TERMINAL
var timee = ("Time is going samh \v what are you doing?");
console.log(timee);
OUTPUT: Time is going samh VT what are you doing?
10. JavaScript String as Object
Normally, JavaScript strings are primitive values.
But strings can also be defined as objects with the keyword new.
// normal string
var str = ("John");
console.log(str);
// output: John
// JavaScript String as Object
var str = new String("John");
console.log(str);
console.log(typeof str);
// output:
// [String: 'John']
// object
let ong = new String("This is an Object String.");
console.log(ong);
// let's check object
console.log(typeof ong);
output:
[String: 'This is an Object String.']
object
Do not create Strings objects.
The new keyword complicates the code and slows down execution speed.
String objects can produce unexpected results.
11. Comparing two JavaScript objects always returns false.
var str1 = new String("This is Object String");
var str2 = new String("This is Object String");
console.log(str1 ===str2);
console.log(str1 ==str2);
12. Check if a string endsWith "family"
The endsWith() method returns true if a string ends with a specified string.
Otherwise it returns false.
The endsWith() method is case sensitive.
var feell = ("I feeling nurvous why? is it for my family")
// console.log(feell.endsWith("why"));
// output: false
console.log(feell.endsWith("family"));
output: true
let ong = new String("This is an Object String.");
let res = ong.endsWith("String.");
console.log(res);
console.log(ong.endsWith("String."))
Check the first 7 character is "This is" or Not
// Check the first 7 character is "This is" or Not
var str2 = "This is a string";
console.log(str2.endsWith("This is",7));
// Check the first 7 character is "This is" or Not
var str2 = "This is a string";
console.log(str2.endsWith("This is",7));
output: true
13. include()
check given word is available or not.
The includes() method returns true if a string contains a specified string.
Otherwise, it returns false.
The includes() method is case sensitive.
// include check given word is available or not.
var ind = "This is a string to check include";
console.log(ind.includes("check"));
output: true
var ind = "This is a string to check include";
console.log(ind.includes("life"));
output: false
14. indexOf()
returns the position of the first occurrence of a value in a string.
The indexOf() method returns the position of the first occurrence of a value in a string.
The indexOf() method returns -1 if the value is not found.
The indexOf() method is case sensitive.
var indexx="Check the indexOf function in string, it is easy."
console.log(indexx.indexOf("the"));
console.log(indexx.indexOf("it"));
console.log(indexx.indexOf("function"));
15. lastIndexOf ()
The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string.
The lastIndexOf() method searches the string from the end to the beginning.
The lastIndexOf() method returns the index from the beginning (position 0).
The lastIndexOf() method returns -1 if the value is not found.
The lastIndexOf() method is case sensitive.
var last = "This is the lastIndexOf method return index position.";
console.log(last.lastIndexOf("the"));
output: 8
16. match()
The match() method matches a string against a regular expression **
The match() method returns an array with the matches.
The match() method returns null if no match is found.
var meth = ("This is ain method in string.");
console.log(meth.match("ain"));
console.log(meth.match("method"));
output:
[
'ain',
index: 8,
input: 'This is ain method in string.',
groups: undefined
]
[
'method',
index: 12,
input: 'This is ain method in string.',
groups: undefined
]
17. String prototype
Use the prototype property to add a new property to all objects of a given type:
function employee(name, Jobtitle,born){
this.name = name;
this.Jobtitle = Jobtitle;
this.born = born;
}
employee.prototype.salary = 2000;
const fred = new employee("Fred Flintstone", "Caveman", 1970);
The prototype is a property available with all JavaScript objects.
The prototype property allows you to add new properties and methods to strings.
17. String repeat
The repeat() method returns a string with a number of copies of a string.
The repeat() method returns a new string.
The repeat() method does not change the original string.
var copyy = ("This is a string we can copy all character.");
console.log(copyy.repeat(5));
output: This is a string we can copy all character.
This is a string we can copy all character.
This is a string we can copy all character.
This is a string we can copy all character.
This is a string we can copy all character.
var str = "Repeat this string 2 times ";
console.log(str.repeat(2));
OUTPUT:
Repeat this string 2 times Repeat this string 2 times
18. String search();
The search() method matches a string against a regular expression **
The search() method returns the index (position) of the first match.
The search() method returns -1 if no match is found.
The search() method is case-sensitive.
var serh = "this is a search method in string";
console.log(serh.search("in"));
OUTPUT: 24
19. String split()
The split() method splits a string into an array of substrings.
The split() method returns the new array.
The split() method does not change the original string.
If (" ") is used as separator, the string is split between words.
var textt = "This is a string character";
var arr = textt.split(" ");
console.log(arr);
output: [ 'This', 'is', 'a', 'string', 'character' ]
var textt = "This is a string character";
var arr = textt.split("");
console.log(arr);
[
'T', 'h', 'i', 's', ' ', 'i',
's', ' ', 'a', ' ', 's', 't',
'r', 'i', 'n', 'g', ' ', 'c',
'h', 'a', 'r', 'a', 'c', 't',
'e', 'r'
]
var textt = "This is a string character";
var arr = textt.split(" ");
var word = arr[3];
console.log(word);
20. String startsWith()
The startsWith() method returns true if a string starts with a specified string.
Otherwise, it returns false.
The startsWith() method is case-sensitive.
var sent = "This is a startWith method";
console.log(sent.startsWith("This"));
console.log(sent.startsWith("is"));
output:
21. substr()
The substr() method extracts a part of a string.
The substr() method begins at a specified position, and returns a specified number of characters.
The substr() method does not change the original string.
To extract characters from the end of the string, use a negative start position.
let subst = "This is a string for subStr check it. How it works";
console.log(subst.substr(5));
output: is a string for subStr check it. How it works
let subst = "This is a string for subStr check it. How it works";
console.log(subst.substr(5,12));
22. toLowerCase() and toUpperCase()
The toLowerCase() method converts a string to lowercase letters.
The toLowerCase() method does not change the original string.
var slt = "This string is written for to test lower case character";
console.log(slt.toLowerCase());
output: this string is written for to test lower case character
var slt = "This string is written for to test lower case character";
var upr = slt.toUpperCase();
console.log(upr);
output: THIS STRING IS WRITTEN FOR TO TEST LOWER CASE CHARACTER
23. trim() method
The trim() method removes whitespace from both sides of a string.
The trim() method does not change the original string
var trrr = " we are going to check trim method";
console.log(trrr);
console.log(trrr.trim());
output:
we are going to check trim method
we are going to check trim method