C++ में String Characters Access करना
In this page:
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 []
// 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;
}
Login to try C/C++/Java/PHP code in the editor
at() से Characters Access करना
at() method [] जैसे ही एक character retrieve करता है, लेकिन bounds checking perform करता है और एक invalid index के लिए undefined behavior cause करने की बजाय एक out_of_range exception throw करता है।
उदाहरण: Accessing Characters with at()
#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;
}
Login to try C/C++/Java/PHP code in the editor
एक Character Modify करना
एक mutable std::string में एक specific character को [] या at() के जरिए एक नई value assign करके बदला जा सकता है, सिर्फ उस एक position को replace करते हुए।
उदाहरण: Modifying a Character
// 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;
}
Login to try C/C++/Java/PHP code in the editor
पहला और आखिरी Characters
front() और back() एक string के पहले और आखिरी characters retrieve करने के convenient shortcuts हैं, str[0] और str[str.length() - 1] के बराबर लेकिन पढ़ने में clearer।
उदाहरण: The First and Last Characters
// 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;
}
Login to try C/C++/Java/PHP code in the editor
हर Character के जरिए Loop करना
0 से length() - 1 तक एक index वाला एक for loop, या एक range-based for loop, order में एक std::string के हर character visit और process करने का standard तरीका है।
उदाहरण: Looping Through Every Character
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक छोटी string पर
s[10]इस्तेमाल करना, जो bounds checked नहीं है और undefined behavior है,s.at(10)की बजाय, जोstd::out_of_rangethrow करता है। - एक empty string पर
s.front()याs[0]call करना, जो undefined behavior है। - last character की तरह
s[s.length()]इस्तेमाल करना, जब last valid indexs.length() - 1है।
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