Show how to use format xtd::format class.
#include <xtd/collections/generic/list>
#include <xtd/console>
#include <xtd/environment>
#include <xtd/format_exception>
#include <xtd/string>
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);}
auto main() -> int {
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;
}
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
#define current_stack_frame_
Provides information about the current stack frame.
Definition current_stack_frame.h:16
@ character
Specifies that the text is trimmed to the nearest character.
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition to_string.h:41
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.h:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10