C++ में Strings और Numbers Convert करना
In this page:
std::string text = std::to_string(number);
int number = std::stoi(text);
एक Number को एक String में Convert करना
std::to_string C++ के किसी भी built-in numeric type को एक std::string में convert करता है, इसका standard text representation produce करते हुए, concatenate या display होने के लिए ready।
उदाहरण: Converting a Number to a String
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
std::string text = std::to_string(age);
// Print to the console with cout
std::cout << text << 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 को एक int में Convert करना
stoi (string to int) एक string के leading numeric हिस्से को parse करता है और इसे एक int की तरह return करता है, अगर string एक valid number से शुरू न हो तो एक exception throw करते हुए।
उदाहरण: Converting a String to an int
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string input = "42";
// Declare number and set it to std::stoi(input)
int number = std::stoi(input);
// Print to the console with cout
std::cout << number + 1 << 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 को एक double में Convert करना
stod (string to double) stoi जैसा काम करता है लेकिन एक floating-point number parse करता है, एक double return करते हुए, उन decimal values पढ़ने के लिए useful जो originally text की तरह enter या store हुई थीं।
उदाहरण: Converting a String to a double
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string input = "3.14";
// Declare value and set it to std::stod(input)
double value = std::stod(input);
// Print to the console with cout
std::cout << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Invalid Conversions Handle करना
stoi और stod दोनों एक std::invalid_argument exception throw करते हैं अगर string बिल्कुल भी एक valid number से शुरू न हो, इसलिए untrusted input से conversions को एक try-catch block में wrap किया जाना चाहिए।
उदाहरण: Handling Invalid Conversions
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string bad = "abc";
// Try code that might throw an exception
try {
// Declare number and set it to std::stoi(bad)
int number = std::stoi(bad);
// Print to the console with cout
std::cout << number << std::endl;
// Handle the exception if one was thrown
} catch (std::invalid_argument &e) {
// Print to the console with cout
std::cout << "Invalid number" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Numbers को Strings की तरह Format करना
to_string को string concatenation के साथ combine करना fixed text को computed numeric values के साथ मिलाने वाला एक formatted message बनाने का एक common तरीका है।
उदाहरण: Formatting Numbers as Strings
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
// Declare score and set it to 95
int score = 95;
std::string message = "Your score: " + std::to_string(score);
// Print to the console with cout
std::cout << message << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
stoi("abc")call करना, जोinvalid_argumentthrow करता है।atoiइस्तेमाल करना और 0 को एक error से न बता पाना।- यह भूल जाना कि
to_stringको<string>चाहिए।
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