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

C++ में String Characters Access करना

एक std::string में individual characters square-bracket indexing या at() method से access किए जा सकते हैं, और उन indices में loop करना order में हर character visit करता है।
Syntax
cpp
variable_name[index];
variable_name.at(index);  // with bounds checking

[] से Characters Access करना

Square-bracket indexing, arrays के लिए इस्तेमाल होने वाला same syntax, एक std::string में एक specific zero-based position पर character retrieve करता है।

उदाहरण: Accessing Characters with []

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[0] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

at() से Characters Access करना

at() method [] जैसे ही एक character retrieve करता है, लेकिन bounds checking perform करता है और एक invalid index के लिए undefined behavior cause करने की बजाय एक out_of_range exception throw करता है।

उदाहरण: Accessing Characters with at()

cpp
#include <iostream>
#include <string>

int main() {
	std::string name = "Alex";
	std::cout << name.at(0) << std::endl;
	// name.at(10); // throws std::out_of_range
	return 0;
}

एक Character Modify करना

एक mutable std::string में एक specific character को [] या at() के जरिए एक नई value assign करके बदला जा सकता है, सिर्फ उस एक position को replace करते हुए।

उदाहरण: Modifying a Character

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";
	name[0] = 'a';
	// Print to the console with cout
	std::cout << name << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

पहला और आखिरी Characters

front() और back() एक string के पहले और आखिरी characters retrieve करने के convenient shortcuts हैं, str[0] और str[str.length() - 1] के बराबर लेकिन पढ़ने में clearer।

उदाहरण: The First and Last Characters

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.front() << " " << name.back() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

हर Character के जरिए Loop करना

0 से length() - 1 तक एक index वाला एक for loop, या एक range-based for loop, order में एक std::string के हर character visit और process करने का standard तरीका है।

उदाहरण: Looping Through Every Character

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

// Program execution starts in main()
int main() {
	std::string word = "Hi!";
	for (char c : word) {
		// Print to the console with cout
		std::cout << c << " ";
	}
	// Print to the console with cout
	std::cout << 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. एक छोटी string पर s[10] इस्तेमाल करना, जो bounds checked नहीं है और undefined behavior है, s.at(10) की बजाय, जो std::out_of_range throw करता है।
  2. एक empty string पर s.front() या s[0] call करना, जो undefined behavior है।
  3. last character की तरह s[s.length()] इस्तेमाल करना, जब last valid index s.length() - 1 है।

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.