C++ में String Concatenation
In this page:
std::string result = string1 + string2;
string1 += string2;
+ से Concatenate करना
std::string के लिए overloaded + operator, दो strings को साथ जोड़कर एक नई combined string बनाता है, दो numbers जोड़ने जितना naturally काम करते हुए।
उदाहरण: Concatenating with +
// 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;
}
Login to try C/C++/Java/PHP code in the editor
+= से Concatenate करना
+= operator एक string को दूसरे के अंत में in place append करता है, एक बिल्कुल नई बनाने की बजाय original string variable को modify करते हुए।
उदाहरण: Concatenating with +=
// 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;
}
Login to try C/C++/Java/PHP code in the editor
कई Strings Concatenate करना
कई strings और literals को एक single expression में + से chain किया जा सकता है, और C++ concatenation को left से right evaluate करता है, final combined string build करते हुए।
उदाहरण: Concatenating Multiple Strings
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Characters Append करना
एक single char को एक std::string में दूसरी string की तरह += से append किया जा सकता है, characters को एक loop के अंदर एक-एक करके build होने देते हुए।
उदाहरण: Appending Characters
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Strings और Non-String Values मिलाना
+ operator एक std::string को सीधे एक raw number के साथ concatenate नहीं कर सकता; number को जोड़ने से पहले पहले एक string में convert करना होगा, आमतौर पर std::to_string से।
उदाहरण: Mixing Strings and Non-String Values
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- दो string literals जोड़ना जैसे
"a" + "b", जो compile नहीं होता क्योंकि दोनोंconst char*हैं। - बहुत बड़ी strings के लिए एक loop में
+इस्तेमाल करना, जोappendसे धीमा है। - एक number को
+से concatenate करना और garbage पाना, जब आपकोstd::to_stringइस्तेमाल करना चाहिए।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings