← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 8 of 15

C++ की STL String

STL string C++ का easy-to-use text type है, text बदलने और search करने के लिए कई built-in tools के साथ। यह आपके लिए memory handle करता है।
Syntax
cpp
std::string string_name = "text";
string_name.append("more");
string_name.insert(position, "text");
string_name.erase(position, count);

String Creation और Basic Methods

std::string text के लिए एक safe, dynamically-resizing container है जो अपनी memory खुद automatically manage करता है, एक raw char array के उलट जिसका एक fixed size है और कोई bounds checking नहीं।

आप एक literal, दूसरी string, या repeated characters से एक बनाते हैं, और class पीछे से allocation handle करती है।

उदाहरण: String Creation and Basic Methods

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

// Program execution starts in main()
int main() {
	std::string name = "Alex";
	// Print to the console with cout
	std::cout << name.length() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

String Modification

append(), insert(), erase(), और operator+= जैसे methods आपको buffer size manually manage किए बिना एक string के contents बढ़ने या सिकुड़ने देते हैं।

क्योंकि std::string खुद resize होता है, ये operations एक fixed-size C-style char array के अंत से आगे लिखने से कहीं ज़्यादा safe हैं।

उदाहरण: String Modification

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

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

Substrings ढूंढना

find() एक character या substring के लिए scan करता है और पहले match का zero-based index return करता है, या अगर कुछ न मिले तो special value std::string::npos -- एक valid index मान लेने की बजाय हमेशा npos के against check करें। rfind() end से same search perform करता है।

उदाहरण: Finding Substrings

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

// Program execution starts in main()
int main() {
	std::string text = "Hello, World!";
	// Declare pos and set it to text.find("World")
	size_t pos = text.find("World");
	if (pos != std::string::npos) std::cout << "Found at " << pos << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Substrings और Comparisons

substr(pos, len) एक दी गई position से शुरू होकर एक नई string extract करता है, जो dates या filenames जैसे fixed-format text parse करने के लिए handy है।

Comparison operators (==, <, >) strings को lexicographically compare करते हैं, character by character, उसी तरह जैसे एक dictionary में words sort होते हैं।

उदाहरण: Substrings and Comparisons

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

// Program execution starts in main()
int main() {
	std::string date = "2024-01-15";
	std::string year = date.substr(0, 4);
	// Print to the console with cout
	std::cout << year << std::endl;
	// Print to the console with cout
	std::cout << (year == "2024") << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Numeric Conversions

std::stoi, std::stod, और similar functions digits रखने वाली एक string को एक int या double में convert करते हैं, अगर text एक valid number न हो तो std::invalid_argument throw करते हुए। std::to_string उल्टा करता है, display या concatenation के लिए एक number को इसके string representation में बदलते हुए।

उदाहरण: Numeric Conversions

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

// Program execution starts in main()
int main() {
	std::string input = "42";
	// Declare number and set it to std::stoi(input)
	int number = std::stoi(input);
	std::string back = std::to_string(number * 2);
	// Print to the console with cout
	std::cout << back << 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. std::string को C-style char* strings से confuse करना।
  2. substr को length से बड़ा एक start index pass करना, जो std::out_of_range throw करता है।
  3. find के result को -1 की बजाय std::string::npos से compare करना।

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.