xxxxxxxxxx
const a = 225; // number
const b = true; // boolean
//converting to string
const result1 = String(a);
const result2 = String(b);
console.log(result1); // "225"
console.log(result2); // "true"
xxxxxxxxxx
//strings example
const name = 'ram';
const name1 = "hari";
const result = `The names are ${name} and ${name1}`;
xxxxxxxxxx
"Hello World"
'Hello World'
"Hello " World"
"Hello World"
"Hello n World"
'Hello ' World'
'Hello t World'
//Concatenation
"Hello" + "World"
var x = "Hello World";
//Length of String
x.length; //It is a property
//Indexing
"Hello World"[0]
"Hello World"[3]
x = "Hello World";
x[0]
x[3]
//Immutable
x[1]
x[1] = "5"
x[1]
// return a new string, doesn't change x
//These are methods
x.slice(1, 5);
x.slice(-6, -1);
x.substr(3, 2); // 2nd parameter means the length
x.replace("Hello", "World");
x.toUpperCase();
x.toLowerCase();
x.concat("1", "2");
x.trim();
//links
//https://www.w3schools.com/jsref/jsref_obj_string.asp
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
xxxxxxxxxx
let myString = "Hoc lap trinh thiet la de"
// => trả về độ dài của mảng
myString.length
// => dùng để tìm kiếm từ trái sang phải nếu không có sẽ trả về -1
myString.indexOf('')
// => dùng để tìm kiếm từ phải sang trái
myString.lastIndexOf('')
// => tìm kiếm theo biểu thức chính quy
myString.search('')
// => dùng để cắt 1 chuỗi
myString.slice(0, 6)
// => dùng để ghi đè (Thêm/.../g để thành biểu thức chính Quy)
myString.replace(/lap trinh/g, 'lập trình')
// => Viết hoa
myString.toUpperCase()
// => Viết thường
myString.toLowerCase()
// => Xóa khoảng trắng đầu và cuối
myString.trim()
let language = 'Javascript, PHP, Ruby'
// => Cắt 1 chuỗi thành Array bằng cách đưa ra điểm chung
language.split(', ')
xxxxxxxxxx
const string1 = "A string primitive";
const string2 = 'Also a string primitive';
const string3 = `Yet another string primitive`;
const string4 = new String("A String object");
xxxxxxxxxx
var myString = "foobar";
//Can use single quotes, double quotes, or backticks for template literals.
xxxxxxxxxx
// JavaScript String
const data =
{
"name": "John Doe",
"age": 45
}
function Sample() { return "TEXT"; }
var str1 = 'With " in it "';
var str2 = "With ' in it '";
var str3 = `Contains other Strings >${str1}<, Properties of objects >${data.age}< or return values from functions >${Sample()}<.`;
console.log(str1); // With " in it "
console.log(str2); // With ' in it '
console.log(str3); // Contains other Strings >With " in it "<, Properties of objects >45< or return values from functions >TEXT<.
"This is my name Anita/n and this is my work"
xxxxxxxxxx
"This is the first line\nAnd this is the second"