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

C++ के String Methods

String methods text के लिए ready-made tools हैं, जैसे इसकी length ढूंढना, अंदर search करना, या एक piece काटना। ये आपको हर letter में खुद loop करने से बचाते हैं।
Syntax
cpp
string_name.length();
string_name.empty();
string_name.method_name(arguments);

String Length ढूंढना

.length() और .size() functionally identical हैं और दोनों एक std::string में currently stored characters की exact संख्या return करते हैं — .size() दूसरे STL containers के साथ consistency के लिए exist करता है, जबकि .length() specifically strings के लिए थोड़ा ज़्यादा naturally पढ़ता है, लेकिन आप दोनों interchangeably इस्तेमाल कर सकते हैं।

उदाहरण: Finding String Length

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

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

Empty Check करना

.empty() true return करता है अगर एक string में zero characters हों और अन्यथा false, और इसे आमतौर पर str.length() == 0 check करने से पसंद किया जाता है, क्योंकि यह एक direct yes/no question की तरह clearly पढ़ता है और कुछ string implementations के लिए marginally ज़्यादा efficient हो सकता है।

उदाहरण: Checking if Empty

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

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

Substrings ढूंढना

.find(substring) एक दी गई substring के पहले occurrence के लिए string search करता है और इसके शुरू होने की index return करता है, या अगर string में कहीं भी कोई match न हो तो special constant string::npos — इसे इस्तेमाल करने से पहले हमेशा result को npos के against check करें, क्योंकि एक failed search error नहीं throw करता, यह बस वह sentinel value return करता है।

उदाहरण: 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 s = "Hello, world";
	// Print to the console with cout
	std::cout << s.find("world") << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Strings Modify करना

.append() एक string के अंत में text add करता है, .insert(pos, text) एक specific index पर text inject करता है बाकी string को shift करते हुए, और .erase(pos, count) एक position से शुरू होकर characters की एक दी गई संख्या हटाता है — साथ में ये आपको strings को piece by piece build और edit करने पर पूरा control देते हैं।

उदाहरण: Modifying Strings

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

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

Substrings पाना

.substr(start, length) string के एक हिस्से की एक copy extract करता है, index start से शुरू होकर length characters तक (या अगर length omitted हो तो अंत तक सब कुछ) — original string पूरी तरह unchanged रहती है, क्योंकि .substr() हमेशा in place modify करने की बजाय एक बिल्कुल नई string return करता है।

उदाहरण: Getting Substrings

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

// Program execution starts in main()
int main() {
	std::string s = "Hello, world";
	// Print to the console with cout
	std::cout << s.substr(7, 5) << 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. यह भूल जाना कि find न मिलने पर string::npos return करता है और इसे गलत तरीके से -1 के तौर पर एक int से compare करना।
  2. substr(pos) के साथ अंत से आगे एक index इस्तेमाल करना, जो out_of_range throw करता है।
  3. यह उम्मीद करना कि s.length() null terminator शामिल करता है।

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.