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

C++ में String Length

length() और size() member functions दोनों एक std::string में characters की संख्या return करते हैं, और empty() check करता है कि एक string में zero characters हैं या नहीं, manual counting के बिना।
Syntax
cpp
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

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;
}

size() Method

size() exactly length() जितनी same value return करता है -- दोनों इसलिए exist करते हैं क्योंकि std::string दूसरे STL containers जैसा same interface implement करता है, जहाँ size() conventional नाम है।

उदाहरण: The size() Method

cpp
#include <iostream>
#include <string>

int main() {
	std::string name = "Alex";
	std::cout << name.size() << std::endl; // same value as length()
	return 0;
}

एक Empty String Check करना

empty() method true return करता है अगर एक string में zero characters हों, जो length() को 0 से compare करने से emptiness check करने का एक clearer और थोड़ा ज़्यादा efficient तरीका है।

उदाहरण: Checking for an Empty String

cpp
// 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;
}

एक Loop में Length इस्तेमाल करना

एक string का length() आमतौर पर एक loop की upper bound की तरह इस्तेमाल होता है जो index से हर character visit करता है, यह पक्का करते हुए कि loop कभी string के अंत से आगे न पढ़े।

उदाहरण: Using Length in a Loop

cpp
// 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;
}

Length बनाम Capacity

एक string का length() यह reflect करता है कि यह actually कितने characters रखता है, जबकि इसकी internal capacity() -- currently reserved memory -- इससे बड़ी हो सकती है, क्योंकि std::string future growth तेज़ बनाने के लिए over-allocate करता है।

उदाहरण: Length vs Capacity

cpp
// 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;
}
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. parentheses के बिना s.length लिखना, जो compile नहीं होता क्योंकि length() एक member function है।
  2. i <= s.length() से loop करना, जो last character से एक आगे जाता है।
  3. एक empty string पर s.length() - 1 compute करना, जहाँ unsigned result एक huge number में wrap हो जाता है।

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.