xtd 0.2.0
Loading...
Searching...
No Matches
format_class_with_specified_formating.cpp

Show how to use format xtd::format class with specified formatting.

#include <xtd/format_exception>
#include <xtd/console>
#include <xtd/environment>
#include <xtd/string>
using namespace xtd;
class character : public iformatable {
public:
character(const string& name, const string& rank) noexcept : name_(name), rank_(rank) {}
const string& name() const noexcept {return name_;}
const string& rank() const noexcept {return rank_;}
string to_string() const noexcept {return to_string("", std::locale {});}
string to_string(const string& format, const std::locale& loc) const override {
auto fmt = string::is_empty(format) ? "F" : format;
if (fmt == "F") return name_ + " (" + rank_ + ")";
if (fmt == "N") return name_;
if (fmt == "R") return rank_;
throw format_exception {};
}
private:
string name_;
string rank_;
};
auto main() -> int {
auto c = character {"Jean-Luc Picard", "Captain"};
console::out << string::format("{}", c) << environment::new_line;
console::out << string::format("{:F}", c) << environment::new_line;
console::out << string::format("{:N}", c) << environment::new_line;
console::out << string::format("{:R}", c) << environment::new_line;
}
// This code produces the following output :
//
// Jean-Luc Picard (Captain)
// Jean-Luc Picard (Captain)
// Jean-Luc Picard
// Captain
The exception that is thrown when the format of an argument does not meet the parameter specification...
Definition format_exception.h:19
Provides functionality to format the value of an object into a string representation.
Definition iformatable.h:35
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10