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

Shows how to use xtd::string::sprintf method.

#include <xtd/collections/generic/list>
#include <xtd/format_exception>
#include <xtd/console>
#include <xtd/environment>
#include <xtd/string>
using namespace xtd;
using namespace xtd::collections;
class character : 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("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 override {
if (fmt == "F") return name_ + " (" + rank_ + ")";
if (fmt == "N") return name_;
if (fmt == "R") return rank_;
}
private:
string name_;
string rank_;
};
using characters = generic::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
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
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::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