String series of characters या group of characters होती है , C++ में string define करने के लिए double quotes का use किया जाता है।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  string message= "Hello !";
  cout << message;
  return 0;
}
Output
Hello !

आप single quotes या backtick का use करके C++ में string define नहीं कर सकते हैं।

string name = `Rahul`

or

string name = 'Rahul' 
//these are invalid string initialization 

C++ String Length

String length जानने के लिए आप length() या size() में से किसी भी function का use कर सकते हैं। दोनों ही same result generate करेंगे।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  string message = "Hello ! how are you ?";
  cout << "lenght : " << message.length() << endl;
  cout << "size : " << message.size();
  return 0;
}
Output
lenght : 21
size : 21

C++ String Concatenation

दो string variables को concat / add के लिए आप plus sign + या append() method का use कर सकते हो।

C++ string concatenation example
CopyFullscreenClose FullscreenRun
#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;
}
Output
Fullname : Rahul Rajput
Fullname : RahulRajput

Example में endl का use line break करने के लिए किया गया है।

C++ numbers and string

आप string को string के साथ plus sign + के through concat कर सकते हैं लेकिन numbers को string के साथ + की help से concat नहीं कर सकते हैं , अगर आप ऐसा करते हैं कुछ इस तरह से error आएगी।

int age = 90;
string info = "Age : " + age;
cout << info;

C++ Access String Character

String characters को आप as a Array elements की तरह ही access कर सकते हैं , Array की तरह ही String में indexing 0 से start होती है।

CopyFullscreenClose FullscreenRun
#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;
}
Output
index 0 : H
index 3 : l
index 9 : o

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers