Repository navigation
Expand file tree
/
Copy path05_Strings_methods.js
More file actions
94 lines (75 loc) · 2.87 KB
/
Copy path05_Strings_methods.js
File metadata and controls
94 lines (75 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// creation of string
let str1 = "hellow";
let str2 = 'world';
let str3 = `hellow ${str2}`;
console.log(str3);
console.log(str1.length);
console.log(str1[0]);
console.log(str1.charAt(1));
// case cpnversion of strings
let name = " aman sharma ";
console.log(name.toUpperCase());
console.log(name.toLowerCase());
//console.log(name.trim());
console.log(name.trimStart());
console.log(name.trimEnd());
// Searching methods of string
let sentence = "java script is easy to learn"
console.log( sentence.indexOf("is"))
console.log(sentence.includes("Easy"))
console.log (sentence.startsWith("Java"))
console.log (sentence.endsWith("learn"))
console.log (sentence.search('easy'))
// slicing aur extracting of the substring
let text = "javascript programming"
console.log (text.slice(0,10))
console.log(text.substring(0,9))
console.log(text.split(" "))
console.log(text.split(" ").join("-"))
//replace repeat and template litral ka use
let msg = " i laove java ";
console.log ( msg.replace("java","javascript"))
console.log("aman".repeat(3));
let user = " Rahul";
let age = 21;
let intro =` my name is ${user} and i am ${age} years old. `;
console.log(intro);
// ================= NAYE 3 REAL-LIFE EXAMPLES =================
// 1. Invoice/Order ID generator (padStart use karke leading zeros)
let orderId = "45";
let formattedId = "ORD-" + orderId.padStart(6, "0");
console.log(formattedId); // ORD-000045
// 2. Credit card number masking (real apps mein aisa hi dikhta hai)
let cardNumber = "1234567890123456";
let last4Digits = cardNumber.slice(-4);
let maskedCard = last4Digits.padStart(cardNumber.length, "*");
console.log(maskedCard); // ************3456
// 3. Phone number ko readable format mein dikhana (padEnd example)
let phone = "98765";
console.log(phone.padEnd(10, "0")); // 9876500000 (dummy example)
// ================= replaceAll aur concat ka use =================
// 4. Sensitive word censor karna puri sentence mein (replaceAll)
let review = "this product is bad, packaging is bad too";
let cleanReview = review.replaceAll("bad", "not good");
console.log(cleanReview);
// 5. Full name banana concat se (form data jodne jaisa real case)
let firstName = "Aman";
let lastName = "Sharma";
let fullName = firstName.concat(" ", lastName);
console.log(fullName); // Aman Sharma
// ================= at() method - real life use =================
// 6. File extension check karna (last character nikal ke)
let fileName = "resume.pdf";
console.log(fileName.at(-1)); // "f" (last char)
console.log(fileName.slice(-3)); // "pdf" (better way for extension)
// Real use case: file type validate karna
function checkFileType(file) {
let ext = file.slice(file.lastIndexOf(".") + 1);
if (ext === "pdf") {
console.log("PDF file hai, upload allowed");
} else {
console.log("Sirf PDF allowed hai");
}
}
checkFileType("resume.pdf");
checkFileType("photo.jpg");