JavaScript Strings
JavaScript web page में string representation को control करने के लिए आपको कई methods provide करती है। इन methods की मदद से आप आसानी से strings पर operations perform कर पाते है। इनके बारे में आपको आगे बताया जायेगा।
Creating JavaScript Strings
var stringName = "text"; |
जैसा की मैने आपको पहले बताया आप strings को single या double quotes में define करते है। इसलिए आप चाहे तो strings को single quotes में भी define कर सकते है।
var stringName = 'text'; |
var name = "Best Hindi Tutorials"; |
Adding Special Characters to JavaScript Strings
var name = "Best "Hindi" Tutorials"; |
JavaScript ऐसे ही कुछ characters को add करने के लिए आपको escape sequence characters provide करती है। इन characters की मदद से आप बिना error generate किये strings create कर सकते है।
JavaScript और दूसरी languages में backslash (\) के साथ escape sequence characters को define किया जाता है। Escape characters से आप दूसरे special characters (@, $ आदि) भी strings में add कर सकते है।
Common javascript escape characters के बारे में निचे दिया जा रहा है।
Escape Characters | Description |
\' | String के अंदर single quote add करने के लिए। |
\" | String के अंदर double quotes add करने के लिए। |
\b | String के अंदर backspace add करने के लिए। |
\r | String के अंदर carriage return add करने के लिए। |
\f | String के अंदर form feed add करने के लिए। |
Length Property
stringName.length; |
इसका उदाहरण निचे दिया जा रहा है।
<!-- javascriptLengthProperty.html --> <html> <body> <script type="text/javascript"> var BHT = "Best Hindi Tutorials"; // Printing length of string document.write(BHT.length); </script> </body> </html> |
20 |
Common String Methods
कुछ common javascript string methods के बारे में निचे दिया जा रहा है।
charAt()
<!-- charatMethodDemo.html --> <html> <script type="text/javascript"> var name = "Vipin"; // Printing character at position 1 var result = name.charAt(1); document.write(result); </script> </html> |
ऊपर दी गयी script निचे दिया गया output generate करती है।
i |
concat()
<!-- concatMethodDemo.html --> <html> <script type="text/javascript"> var fname = "Vipin " var lname = "Sharma"; // Concatenating first name and last name var result = fname.concat(lname); document.write(result); </script> </html> |
Vipin Sharma |
endsWith()
<!-- endWithMethodDemo.html --> <html> <script type="text/javascript"> var BHT = "Best Hindi Tutorials"; // Checking what a string ends with. var result = BHT.endsWith("Tutorials"); document.write(result); </script> </html> |
true |
includes()
<!-- includesMethodDemo.html --> <html> <script type="text/javascript"> var name = "Best Hindi Tutorials"; // Checking if a string includes a word. var result = name.includes("Hindi"); document.write(result); </script> </html> |
true |
indexOf()
<!-- indexOfMethodDemo.html --> <html> <script type="text/javascript"> var bht = "Best Hindi Tutorials"; // Getting starting index of a word. var result = bht.indexOf("Hindi"); document.write(result); </script> </html> |
5 |
search()
<!-- searchMethodDemo.html --> <html> <script type="text/javascript"> var str = "Best Hindi Tutorials"; // Searching starting index of a word. var result = str.search("Tutorials"); document.write(result); </script> </html> |
ऊपर दी गयी script निचे दिया गया output generate करती है।
11 |
replace()
<!-- replaceMethodDemo.html --> <html> <script type="text/javascript"> var bht = "Best Hindi Tutorials Ever"; // Replacing a word var result = bht.replace("Hindi"," "); document.write(result); </script> </html> |
ऊपर दी गयी script निचे दिया गया output generate करती है।
Best Tutorials Ever |
substr()
इसका उदाहरण निचे दिया जा रहा है।
<!-- substrMethodDemo.html --> <html> <script type="text/javascript"> var bht = "Best Hindi Tutorials"; // Getting substring from a string var result = bht.substr(5,5); document.write(result); </script> </html> |
ऊपर दी गयी script निचे दिया गया output generate करती है।
Hindi |
toLowerCase()
<!-- toLowerCaseMethodDemo.html --> <html> <script type="text/javascript"> var bht = "BEST HINDI TUTORIALS"; // Converting string to lowercase var result = bht.toLowerCase(); document.write(result); </script> </html> |
best hindi tutorials |
toUpperCase()
<!-- toUpperCaseMethodDemo.html --> <html> <script type="text/javascript"> var str = "best hindi tutorials"; // Converting string to upper case var res = str.toUpperCase(); document.write(res); </script> </html> |