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

Show how to use format xtd::format class.

#include <xtd/xtd>
class character {
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("F", std::locale {});}
string to_string(const string& fmt) const noexcept {return to_string(fmt, std::locale {});}
string to_string(const string& fmt, const std::locale& loc) const {
if (fmt == "F") return name_ + " (" + rank_ + ")";
if (fmt == "N") return name_;
if (fmt == "R") return rank_;
}
private:
string name_;
string rank_;
};
template<>
std::string xtd::to_string(const character& value, const std::string& fmt, const std::locale& loc) {return value.to_string(fmt, loc);}
using characters = list<character>;
auto main() -> int {
character c("Jean-Luc Picard", "Captain");
console::out << string::sprintf("%s", c.to_string()) << environment::new_line;
console::out << string::sprintf("%s", c.to_string("F")) << environment::new_line;
console::out << string::sprintf("%s", c.to_string("N")) << environment::new_line;
console::out << string::sprintf("%s", c.to_string("R")) << 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.hpp:18
@ character
Specifies that the text is trimmed to the nearest character.
Definition string_trimming.hpp:21
@ c
The C key.
Definition console_key.hpp:92
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375