C++ के String Methods
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Substrings पाना
.substr(start, length) string के एक हिस्से की एक copy extract करता है, index start से शुरू होकर length characters तक (या अगर length omitted हो तो अंत तक सब कुछ) — original string पूरी तरह unchanged रहती है, क्योंकि .substr() हमेशा in place modify करने की बजाय एक बिल्कुल नई string return करता है।
उदाहरण: Getting Substrings
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि
findन मिलने परstring::nposreturn करता है और इसे गलत तरीके से -1 के तौर पर एक int से compare करना। substr(pos)के साथ अंत से आगे एक index इस्तेमाल करना, जोout_of_rangethrow करता है।- यह उम्मीद करना कि
s.length()null terminator शामिल करता है।
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