← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 9 of 15

C++ में String Concatenation

std::string + और += operators के साथ concatenation support करता है, strings और यहाँ तक कि individual characters को एक special function call किए बिना सीधे साथ जोड़ने देते हुए।
Syntax
cpp
std::string result = string1 + string2;

string1 += string2;

+ से Concatenate करना

std::string के लिए overloaded + operator, दो strings को साथ जोड़कर एक नई combined string बनाता है, दो numbers जोड़ने जितना naturally काम करते हुए।

उदाहरण: Concatenating with +

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string first = "Hello, ";
	std::string second = "world!";
	std::string result = first + second;
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

+= से Concatenate करना

+= operator एक string को दूसरे के अंत में in place append करता है, एक बिल्कुल नई बनाने की बजाय original string variable को modify करते हुए।

उदाहरण: Concatenating with +=

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string message = "Hello";
	message += ", world!";
	// Print to the console with cout
	std::cout << message << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Strings Concatenate करना

कई strings और literals को एक single expression में + से chain किया जा सकता है, और C++ concatenation को left से right evaluate करता है, final combined string build करते हुए।

उदाहरण: Concatenating Multiple Strings

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string result = "A" + std::string("B") + "C" + "D";
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Characters Append करना

एक single char को एक std::string में दूसरी string की तरह += से append किया जा सकता है, characters को एक loop के अंदर एक-एक करके build होने देते हुए।

उदाहरण: Appending Characters

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string word = "";
	word += 'H';
	word += 'i';
	// Print to the console with cout
	std::cout << word << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Strings और Non-String Values मिलाना

+ operator एक std::string को सीधे एक raw number के साथ concatenate नहीं कर सकता; number को जोड़ने से पहले पहले एक string में convert करना होगा, आमतौर पर std::to_string से।

उदाहरण: Mixing Strings and Non-String Values

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	std::string message = "Age: " + std::to_string(age);
	// Print to the console with cout
	std::cout << message << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. दो string literals जोड़ना जैसे "a" + "b", जो compile नहीं होता क्योंकि दोनों const char* हैं।
  2. बहुत बड़ी strings के लिए एक loop में + इस्तेमाल करना, जो append से धीमा है।
  3. एक number को + से concatenate करना और garbage पाना, जब आपको std::to_string इस्तेमाल करना चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.