C++ में String Length
In this page:
variable_name.length();
variable_name.size();
length() Method
length() member function एक std::string में currently stored characters की संख्या return करता है, किसी hidden terminator को शामिल किए बिना, क्योंकि std::string अपना size internally manage करता है।
उदाहरण: The length() Method
// 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;
}
Login to try C/C++/Java/PHP code in the editor
size() Method
size() exactly length() जितनी same value return करता है -- दोनों इसलिए exist करते हैं क्योंकि std::string दूसरे STL containers जैसा same interface implement करता है, जहाँ size() conventional नाम है।
उदाहरण: The size() Method
#include <iostream>
#include <string>
int main() {
std::string name = "Alex";
std::cout << name.size() << std::endl; // same value as length()
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Empty String Check करना
empty() method true return करता है अगर एक string में zero characters हों, जो length() को 0 से compare करने से emptiness check करने का एक clearer और थोड़ा ज़्यादा efficient तरीका है।
उदाहरण: Checking for an Empty String
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string name = "";
// Check whether name.empty()
if (name.empty()) {
// Print to the console with cout
std::cout << "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
एक Loop में Length इस्तेमाल करना
एक string का length() आमतौर पर एक loop की upper bound की तरह इस्तेमाल होता है जो index से हर character visit करता है, यह पक्का करते हुए कि loop कभी string के अंत से आगे न पढ़े।
उदाहरण: Using Length in a Loop
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string word = "Hello";
// Loop: repeat while the condition holds
for (int i = 0; i < word.length(); i++) {
// Print to the console with cout
std::cout << word[i] << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Length बनाम Capacity
एक string का length() यह reflect करता है कि यह actually कितने characters रखता है, जबकि इसकी internal capacity() -- currently reserved memory -- इससे बड़ी हो सकती है, क्योंकि std::string future growth तेज़ बनाने के लिए over-allocate करता है।
उदाहरण: Length vs Capacity
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string s = "Hi";
// Print to the console with cout
std::cout << "length=" << s.length() << " capacity=" << s.capacity() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- parentheses के बिना
s.lengthलिखना, जो compile नहीं होता क्योंकिlength()एक member function है। i <= s.length()से loop करना, जो last character से एक आगे जाता है।- एक empty string पर
s.length() - 1compute करना, जहाँ unsigned result एक huge number में wrap हो जाता है।
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