JavaScript String charAt()
let text = "HELLO WORLD";
let char = text.charAt(0);
JavaScript String charCodeAt()
let text = "HELLO WORLD";
let char = text.charCodeAt(0);
JavaScript codePointAt()
let text = "HELLO WORLD";
let code = text.codePointAt(0);
let code = text.codePointAt(0);
JavaScript String at()
const name = "W3Schools";
let letter = name.at(2);
let letter = name.at(2);
Get the third letter of name
const name = "W3Schools";
let letter = name[2];
let letter = name[2];
Immutable
let text = "HELLO WORLD";
text[0] = "A"; // Gives no error, but does not work
text[0] = "A"; // Gives no error, but does not work
JavaScript String concat()
let text1 = "Hello";
let text2 = "World";
let text3 = text1.concat(" ", text2);
let text2 = "World";
let text3 = text1.concat(" ", text2);
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.
Extracting String Parts
There are 3 methods for extracting a part of a string:
slice(start, end)substring(start, end)substr(start, length)
let text = "Apple, Banana, Kiwi";
let part = text.slice(7, 13);
let part = text.slice(7, 13);
let text = "Apple, Banana, Kiwi";
let part = text.slice(7);
let part = text.slice(7);
let text = "Apple, Banana, Kiwi";
let part = text.slice(-12);
let part = text.slice(-12);
let text = "Apple, Banana, Kiwi";
let part = text.slice(-12, -6);
let part = text.slice(-12, -6);
JavaScript String substring()
let str = "Apple, Banana, Kiwi";
let part = str.substring(7, 13);
let part = str.substring(7, 13);
No comments:
Post a Comment