If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
String series of characters या group of characters होती है , C++ में string define करने के लिए double quotes का use किया जाता है।
#include <iostream>
using namespace std;
int main() {
string message= "Hello !";
cout << message;
return 0;
}
Hello !
आप single quotes या backtick का use करके C++ में string define नहीं कर सकते हैं।
string name = `Rahul` or string name = 'Rahul' //these are invalid string initialization
String length जानने के लिए आप length() या size() में से किसी भी function का use कर सकते हैं। दोनों ही same result generate करेंगे।
#include <iostream>
using namespace std;
int main() {
string message = "Hello ! how are you ?";
cout << "lenght : " << message.length() << endl;
cout << "size : " << message.size();
return 0;
}
lenght : 21 size : 21
दो string variables को concat / add के लिए आप plus sign + या append() method का use कर सकते हो।
#include <iostream>
using namespace std;
int main() {
string f_name = "Rahul";
string l_name = "Rajput";
// concat using + sign and use endl for line break.
cout << "Fullname : " << f_name + " " + l_name << endl;
// now use append() function.
cout << "Fullname : " << f_name.append(l_name);
return 0;
}
Fullname : Rahul Rajput Fullname : RahulRajput
Example में endl का use line break करने के लिए किया गया है।
आप string को string के साथ plus sign + के through concat कर सकते हैं लेकिन numbers को string के साथ + की help से concat नहीं कर सकते हैं , अगर आप ऐसा करते हैं कुछ इस तरह से error आएगी।
int age = 90; string info = "Age : " + age; cout << info;
String characters को आप as a Array elements की तरह ही access कर सकते हैं , Array की तरह ही String में indexing 0 से start होती है।
#include <iostream>
using namespace std;
int main() {
string message = "Hello ! how are you ?";
cout << "index 0 : " << message[0] << endl;
cout << "index 3 : " << message[3] << endl;
cout << "index 9 : " << message[9];
return 0;
}
index 0 : H index 3 : l index 9 : o
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook