Introduction
हेलो दोस्तों आज हम इस post में javascript string के बारेमे जानेगे!. javascript में string किसे कहते है और javascript में string को कैसे create किया जाता है|. String object का इस्तेमाल characters sequence को represent और manipulate करने के लिए होता है|. String data को रखने के लिए उपयोगी होते है जिन्हे text form में represent कर सकते है|. string के लिए सबसे अधिक इस्तेमाल किये जाने वाले कुछ operations जैसे की string की length find करना, दो और उससे अधिक string को जोड़ने के लिए + string + operator का उपयोग किया जाता है|. किसी string sequence में specific word का position find करने के लिए indexof() method का इस्तेमाल किया जाता है|. उसके आलावा बी string के कई operation perform करा सकते है|.
What is string in javascript (javascript में string क्या है?)
String एक character का collection होता है या इसे हम इस तरह बी कह सकते है की string character का एक sequence होता है|. मान लीजिये हमें कुछ लिखना है जैसे की ” Hello World “ तो उसे हम एक sring कह सकते है|. string को single और double quotes में लिख सकते है|. string को एक variable में store करा सकते है|.
Example:
let string = "Hello World"; console.log( string );
Creating strings in javascript
Javascript में string को कैसे create कर सकते है और created string को variable में कैसे store करा सकते है वह आप देख सकते है|.
1. Single qoutes string
String को single qoutes के साथ create करके, variable में store करना
let str1 = 'This is string of javascript';
2. Double qoutes string
String को double qoutes के साथ create करके, variable में store करना
let str2 = "This is string of javascript ";
3. Template literals
Template literals में string को back-tick ( ` ) के इस्तेमाल से define किया जाता है|, variable में store करना
let str3 = `This is string of javascript`;
Template literals में single और double quoted string को रख सकते है|.
let str = `This "is" string of javascript`;
Template literals में variable को directly insert करा सकते है|.
let str = "javascript"; let newstr = `This "is" string of ${str }`;
Template literals में variable को ${varianblename} के साथ directly access कर सकते है|.
Escape Sequence character
String को हमेशा quotes के भीतर लिखा जाता है. लेकिन कई बार हम कुछ ऐसी string को print करना चाहते है जैसे की
let str ='It's javascript string';
इस case में हम string को print नहीं करा सकते तो इस case में हम Escape Sequence character का इस्तेमाल करते है|.
let str ='It\'s javascript string';
और ऐसी तरह हम \”” का इस्तेमाल double quotes में कर सकते है|.
दूसरे Escape Sequence character जैसे की
- \n => new line
- \t => Horizontal tabulator
- \r => Carriage return
String character को access कैसे कर सकते है?
String के individual character को हम दो तरीके से access कर सकते है|.
charAt() method से
let str = 'Hello World'; console.log( str.charAt(1) );
दुसरे तरीके में string को array और object के रूप में मानना है|
String के हर एक character specific index में define हुवे होते है और indexing हमेशा [0] से start होती है|.
let str = "student"; console.log( str[0] ); console.log( str[1] );