用C ++填充stl字符串

时间:2009-03-20 17:38:51

标签: c++ string padding stdstring

我正在使用std::string并需要将它们填充到给定的宽度。在C ++中这样做的推荐方法是什么?

示例输入:

123

填充到10个字符。

示例输出:

       123

(123前面7个空格)

14 个答案:

答案 0 :(得分:58)

std :: setw(setwidth)操纵器

std::cout << std::setw (10) << 77 << std::endl;

std::cout << std::setw (10) << "hi!" << std::endl;

输出填充77和“hi!”。

如果你需要结果作为字符串使用std :: stringstream的实例而不是std :: cout对象。

ps:负责的头文件<iomanip>

答案 1 :(得分:39)

void padTo(std::string &str, const size_t num, const char paddingChar = ' ')
{
    if(num > str.size())
        str.insert(0, num - str.size(), paddingChar);
}

int main(int argc, char **argv)
{
    std::string str = "abcd";
    padTo(str, 10);
    return 0;
}

答案 2 :(得分:22)

你可以像这样使用它:

std::string s = "123";
s.insert(s.begin(), paddedLength - s.size(), ' ');

答案 3 :(得分:21)

我能想到的最简单的方法是使用stringstream:

string foo = "foo";
stringstream ss;
ss << setw(10) << foo;
foo = ss.str();

foo现在应该填充。

答案 4 :(得分:7)

您可以通过调用

创建一个包含N个空格的字符串
string(N, ' ');

所以你可以这样做:

string to_be_padded = ...;
if (to_be_padded.size() < 10) {
  string padded(10 - to_be_padded.size(), ' ');
  padded += to_be_padded;
  return padded;
} else { return to_be_padded; }

答案 5 :(得分:5)

std::string pad_right(std::string const& str, size_t s)
{
    if ( str.size() < s )
        return str + std::string(s-str.size(), ' ');
    else
        return str;
}

std::string pad_left(std::string const& str, size_t s)
{
    if ( str.size() < s )
        return std::string(s-str.size(), ' ') + str;
    else
        return str;
}

答案 6 :(得分:2)

有一种很简单的方法:)

const int required_pad = 10;

std::string myString = "123";
size_t length = myString.length();

if (length < required_pad)
  myString.insert(0, required_pad - length, ' ');

答案 7 :(得分:2)

最低工作代码:

#include <iostream>
#include <iomanip>

int main()
{
    for(int i = 0; i < 300; i += 11)
    {
        std::cout << std::setfill ( ' ' ) << std::setw (2) << (i % 100) << std::endl;
    }
    return 0;
}

/*
Note:
- for std::setfill ( ' ' ):
  - item in '' is what will be used for filling
- std::cout may be replaced with a std::stringstream if you need it
- modulus is used to cut the integer to an appropriate length, for strings use substring
- std::setw is used to define the length of the needed string
*/

答案 8 :(得分:1)

怎么样:

string s = "          "; // 10 spaces
string n = "123";
n.length() <= 10 ? s.replace(10 - n.length(), n.length(), s) : s = n;

答案 9 :(得分:0)

我正在寻找主题是因为我正在开发VCL。无论如何,创建一个函数并不难。

void addWhiteSpcs(string &str, int maxLength) {
    int i, length;

    length = str.length();
    for(i=length; i<maxLength; i++)
    str += " ";
};

string name1 = "johnny";
string name2 = "cash";

addWhiteSpcs(name1, 10);
addWhiteSpcs(name2, 10);

在两种情况下,它将添加到右边的10个空格。我建议使用诸如Courier或consolas之类的等宽字体来获取正确的格式。

这是当您不使用等宽字体时发生的情况
约翰尼____
现金______

// using monospace font the output will be
johnny____
cash______

两个案例的长度相同。

答案 10 :(得分:0)

@Configuration
public class AppConfig {

   @Value("#{'${jobs.max-num-of-threads:}' ?: @appConfig.DEFAULT_MAX_NUM_OF_THREADS}")
   private Integer maxNumOfThreads;

   public final Integer DEFAULT_MAX_NUM_OF_THREADS = 10;

   @PostConstruct
   public void init(){
       System.out.println("@PostConstruct init() - in.");
   }
}

Try it online!

答案 11 :(得分:0)

https://repl.it/@JomaCorpFX/Padding#main.cpp

上测试此代码

我们有两种类型的填充

  • 普通填充

它需要字符串的每个字符一个单独的元素

std::string str = u8"Hello"; //5 elements
std::string str2 = u8"Hello ?"; //10 elements // ? has 4 chars.
std::string str3 = u8"?"; //4 elements // ? has 4 chars.
  • UTF8 填充

长度为UTF8单位,一个UTF8单位可以是1-4个字符

std::string str = u8"Hello"; //5 UTF8 units.
std::string str2 = u8"Hello ?"; //7 UTF8 units // ? is an individual element.
std::string str3 = u8"?"; //1 UTF8 units

功能

class Strings
{

    static size_t LengthUTF8(const String& data);

    static String SubstringUTF8(const String& data, size_t startIndex, size_t length);

    static String PadRight(const String& data, const size_t& totalWidth, const char& padding);

    static String PadLeft(const String& data, const size_t& totalWidth, const char& padding);

    static String PadRight(const String& data, const size_t& totalWidth, const String& padding);

    static String PadLeft(const String& data, const size_t& totalWidth, const String& padding);

    static String PadRightUTF8(const String& data, const size_t& totalWidth, const char& padding);

    static String PadLeftUTF8(const String& data, const size_t& totalWidth, const char& padding);

    static String PadRightUTF8(const String& data, const size_t& totalWidth, const String& padding);

    static String PadLeftUTF8(const String& data, const size_t& totalWidth, const String& padding);
};

以 UTF8 结尾的函数以 UTF8 为单位处理字符串以确定其长度。不以UTF8结尾的函数会以每个char为单位来确定长度。

如果 padding 是字符串,如果是 UTF8 函数,则以 UTF8 为单位处理,否则按字符处理。

参数 totalWidth 是基于函数 normal 或 UTF8。

完整代码


#include <iostream>
#include <set>
#include <string>
#include <locale>

// WINDOWS
#if (_WIN32)
#include <Windows.h>
#include <conio.h>
#define WINDOWS_PLATFORM 1
#define DLLCALL STDCALL
#define DLLIMPORT _declspec(dllimport)
#define DLLEXPORT _declspec(dllexport)
#define DLLPRIVATE
#define NOMINMAX

//EMSCRIPTEN
#elif defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
#include <emscripten/bind.h>
#include <unistd.h>
#include <termios.h>
#define EMSCRIPTEN_PLATFORM 1
#define DLLCALL
#define DLLIMPORT
#define DLLEXPORT __attribute__((visibility("default")))
#define DLLPRIVATE __attribute__((visibility("hidden")))

// LINUX - Ubuntu, Fedora, , Centos, Debian, RedHat
#elif (__LINUX__ || __gnu_linux__ || __linux__ || __linux || linux)
#define LINUX_PLATFORM 1
#include <unistd.h>
#include <termios.h>
#define DLLCALL CDECL
#define DLLIMPORT
#define DLLEXPORT __attribute__((visibility("default")))
#define DLLPRIVATE __attribute__((visibility("hidden")))
#define CoTaskMemAlloc(p) malloc(p)
#define CoTaskMemFree(p) free(p)

//ANDROID
#elif (__ANDROID__ || ANDROID)
#define ANDROID_PLATFORM 1
#define DLLCALL
#define DLLIMPORT
#define DLLEXPORT __attribute__((visibility("default")))
#define DLLPRIVATE __attribute__((visibility("hidden")))

//MACOS
#elif defined(__APPLE__)
#include <unistd.h>
#include <termios.h>
#define DLLCALL
#define DLLIMPORT
#define DLLEXPORT __attribute__((visibility("default")))
#define DLLPRIVATE __attribute__((visibility("hidden")))
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR
#define IOS_SIMULATOR_PLATFORM 1
#elif TARGET_OS_IPHONE
#define IOS_PLATFORM 1
#elif TARGET_OS_MAC
#define MACOS_PLATFORM 1
#else

#endif

#endif



typedef std::string String;
typedef std::wstring WString;

#define EMPTY_STRING u8""s
#define EMPTY_WSTRING L""s

using namespace std::literals::string_literals;

class Strings
{
public:
    static String WideStringToString(const WString& wstr)
    {
        if (wstr.empty())
        {
            return String();
        }
        size_t pos;
        size_t begin = 0;
        String ret;

#if WINDOWS_PLATFORM
        int size;
        pos = wstr.find(static_cast<wchar_t>(0), begin);
        while (pos != WString::npos && begin < wstr.length())
        {
            WString segment = WString(&wstr[begin], pos - begin);
            size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, &segment[0], segment.size(), NULL, 0, NULL, NULL);
            String converted = String(size, 0);
            WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, &segment[0], segment.size(), &converted[0], converted.size(), NULL, NULL);
            ret.append(converted);
            ret.append({ 0 });
            begin = pos + 1;
            pos = wstr.find(static_cast<wchar_t>(0), begin);
        }
        if (begin <= wstr.length())
        {
            WString segment = WString(&wstr[begin], wstr.length() - begin);
            size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, &segment[0], segment.size(), NULL, 0, NULL, NULL);
            String converted = String(size, 0);
            WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, &segment[0], segment.size(), &converted[0], converted.size(), NULL, NULL);
            ret.append(converted);
        }
#elif LINUX_PLATFORM || MACOS_PLATFORM || EMSCRIPTEN_PLATFORM
        size_t size;
        pos = wstr.find(static_cast<wchar_t>(0), begin);
        while (pos != WString::npos && begin < wstr.length())
        {
            WString segment = WString(&wstr[begin], pos - begin);
            size = wcstombs(nullptr, segment.c_str(), 0);
            String converted = String(size, 0);
            wcstombs(&converted[0], segment.c_str(), converted.size());
            ret.append(converted);
            ret.append({ 0 });
            begin = pos + 1;
            pos = wstr.find(static_cast<wchar_t>(0), begin);
        }
        if (begin <= wstr.length())
        {
            WString segment = WString(&wstr[begin], wstr.length() - begin);
            size = wcstombs(nullptr, segment.c_str(), 0);
            String converted = String(size, 0);
            wcstombs(&converted[0], segment.c_str(), converted.size());
            ret.append(converted);
        }
#else
        static_assert(false, "Unknown Platform");
#endif
        return ret;
    }

    static WString StringToWideString(const String& str)
    {
        if (str.empty())
        {
            return WString();
        }

        size_t pos;
        size_t begin = 0;
        WString ret;
#ifdef WINDOWS_PLATFORM
        int size = 0;
        pos = str.find(static_cast<char>(0), begin);
        while (pos != std::string::npos) {
            std::string segment = std::string(&str[begin], pos - begin);
            std::wstring converted = std::wstring(segment.size() + 1, 0);
            size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, &segment[0], segment.size(), &converted[0], converted.length());
            converted.resize(size);
            ret.append(converted);
            ret.append({ 0 });
            begin = pos + 1;
            pos = str.find(static_cast<char>(0), begin);
        }
        if (begin < str.length()) {
            std::string segment = std::string(&str[begin], str.length() - begin);
            std::wstring converted = std::wstring(segment.size() + 1, 0);
            size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, segment.c_str(), segment.size(), &converted[0], converted.length());
            converted.resize(size);
            ret.append(converted);
        }

#elif LINUX_PLATFORM || MACOS_PLATFORM || EMSCRIPTEN_PLATFORM
        size_t size;
        pos = str.find(static_cast<char>(0), begin);
        while (pos != String::npos)
        {
            String segment = String(&str[begin], pos - begin);
            WString converted = WString(segment.size(), 0);
            size = mbstowcs(&converted[0], &segment[0], converted.size());
            converted.resize(size);
            ret.append(converted);
            ret.append({ 0 });
            begin = pos + 1;
            pos = str.find(static_cast<char>(0), begin);
        }
        if (begin < str.length())
        {
            String segment = String(&str[begin], str.length() - begin);
            WString converted = WString(segment.size(), 0);
            size = mbstowcs(&converted[0], &segment[0], converted.size());
            converted.resize(size);
            ret.append(converted);
        }
#else
        static_assert(false, "Unknown Platform");
#endif
        return ret;
    }

    static size_t LengthUTF8(const String& data)
    {
        size_t ret = 0;
        for (char value : data)
        {
            if ((value & 0xc0) != 0x80)
            {
                ++ret;
            }
        }
        return ret;
    }

    static String SubstringUTF8(const String& data, size_t startIndex, size_t length)
    {
        size_t units = LengthUTF8(data);
        if (startIndex >= units)
        {
            throw std::invalid_argument(u8"Invalid UTF8 character position.");
        }
        String result;
        String::const_iterator it = data.begin();
        String::const_iterator beginIterator = data.begin();
        size_t endIndex = length == SIZE_MAX ? units : startIndex + length;
        size_t utf8pos = 0;
        while (it != data.end())
        {
            char value = *it;
            if ((value & 0xc0) != 0x80) // Es inicio de caracter //utf8pos = 0 / beginIterator = 0 / endIndex = 1+12
            {
                if (utf8pos == startIndex)
                {
                    beginIterator = it;
                    if (length >= units)
                    {
                        return String(beginIterator, data.end());
                    }
                }

                if (utf8pos == endIndex)
                {
                    break;
                }
                utf8pos += 1;
            }
            it++;
        }
        return String(beginIterator, it);
    }

    static String GetCharUTF8(const String& data, size_t pos)
    {
        return SubstringUTF8(data, pos, 1);
    }

    static String PadRight(const String& data, const size_t& totalWidth, const char& padding)
    {
        if (data.length() >= totalWidth)
        {
            return data;
        }
        String ret = data;
        ret.resize(totalWidth, padding);
        return ret;
    }

    static String PadLeft(const String& data, const size_t& totalWidth, const char& padding)
    {
        if (data.length() >= totalWidth)
        {
            return data;
        }
        String ret = data;
        ret.insert(0, totalWidth - ret.length(), padding);
        return ret;
    }

    static String PadRight(const String& data, const size_t& totalWidth, const String& padding)
    {

        if (data.length() >= totalWidth)
        {
            return data;
        }
        size_t modulo = (totalWidth - data.length()) % padding.length();
        size_t paddingUnits = (totalWidth - data.length()) / padding.length();
        String ret = data;
        for (size_t i = 0; i < paddingUnits; i++)
        {
            ret.append(padding);
        }
        ret.append(padding.substr(0, modulo));
        return ret;
    }

    static String PadLeft(const String& data, const size_t& totalWidth, const String& padding)
    {
        if (data.length() >= totalWidth)
        {
            return data;
        }
        size_t modulo = (totalWidth - data.length()) % padding.length();
        size_t paddingUnits = (totalWidth - data.length()) / padding.length();
        String ret = data;
        for (size_t i = 0; i < paddingUnits; i++)
        {
            ret.insert(0, padding);
        }
        ret.insert(0, padding.substr(0, modulo));
        return ret;
    }

    static String PadRightUTF8(const String& data, const size_t& totalWidth, const char& padding)
    {
        size_t totalUtf8 = LengthUTF8(data);
        if (totalUtf8 >= totalWidth)
        {
            return data;
        }
        String ret = data;
        ret.resize(ret.length() + (totalWidth - totalUtf8), padding);
        return ret;
    }

    static String PadLeftUTF8(const String& data, const size_t& totalWidth, const char& padding)
    {
        size_t totalUtf8 = LengthUTF8(data);
        if (totalUtf8 >= totalWidth)
        {
            return data;
        }
        String ret = data;
        ret.insert(0, totalWidth - totalUtf8, padding);
        return ret;
    }

    static String PadRightUTF8(const String& data, const size_t& totalWidth, const String& padding)
    {
        size_t units = LengthUTF8(data);
        size_t paddingUnits = LengthUTF8(padding);
        if (units >= totalWidth)
        {
            return data;
        }
        size_t modulo = (totalWidth - units) % paddingUnits;
        size_t n = (totalWidth - units) / paddingUnits;
        String ret = data;
        for (size_t i = 0; i < n; i++)
        {
            ret.append(padding);
        }
        ret.append(SubstringUTF8(padding, 0, modulo));
        return ret;
    }

    static String PadLeftUTF8(const String& data, const size_t& totalWidth, const String& padding)
    {
        size_t units = LengthUTF8(data);
        size_t paddingUnits = LengthUTF8(padding);
        if (units >= totalWidth)
        {
            return data;
        }
        size_t modulo = (totalWidth - units) % paddingUnits;
        size_t n = (totalWidth - units) / paddingUnits;
        String ret = data;
        for (size_t i = 0; i < n; i++)
        {
            ret.insert(0, padding);
        }
        ret.insert(0, SubstringUTF8(padding, 0, modulo));
        return ret;
    }


};

enum class ConsoleTextStyle
{
    DEFAULT = 0,
    BOLD = 1,
    FAINT = 2,
    ITALIC = 3,
    UNDERLINE = 4,
    SLOW_BLINK = 5,
    RAPID_BLINK = 6,
    REVERSE = 7,
};

enum class ConsoleForeground
{
    DEFAULT = 39,
    BLACK = 30,
    DARK_RED = 31,
    DARK_GREEN = 32,
    DARK_YELLOW = 33,
    DARK_BLUE = 34,
    DARK_MAGENTA = 35,
    DARK_CYAN = 36,
    GRAY = 37,
    DARK_GRAY = 90,
    RED = 91,
    GREEN = 92,
    YELLOW = 93,
    BLUE = 94,
    MAGENTA = 95,
    CYAN = 96,
    WHITE = 97
};

enum class ConsoleBackground
{
    DEFAULT = 49,
    BLACK = 40,
    DARK_RED = 41,
    DARK_GREEN = 42,
    DARK_YELLOW = 43,
    DARK_BLUE = 44,
    DARK_MAGENTA = 45,
    DARK_CYAN = 46,
    GRAY = 47,
    DARK_GRAY = 100,
    RED = 101,
    GREEN = 102,
    YELLOW = 103,
    BLUE = 104,
    MAGENTA = 105,
    CYAN = 106,
    WHITE = 107
};

class Console //For Printing Unicode strings.
{
private:
    static void EnableVirtualTermimalProcessing()
    {
#if defined WINDOWS_PLATFORM
        HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        DWORD dwMode = 0;
        GetConsoleMode(hOut, &dwMode);
        if (!(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
        {
            dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
            SetConsoleMode(hOut, dwMode);
        }
#endif
    }

    static void ResetTerminalFormat()
    {
        std::cout << u8"\033[0m";
    }

    static void SetVirtualTerminalFormat(ConsoleForeground foreground, ConsoleBackground background, std::set<ConsoleTextStyle> styles)
    {
        String format = u8"\033[";
        format.append(std::to_string(static_cast<int>(foreground)));
        format.append(u8";");
        format.append(std::to_string(static_cast<int>(background)));
        if (styles.size() > 0)
        {
            for (auto it = styles.begin(); it != styles.end(); ++it)
            {
                format.append(u8";");
                format.append(std::to_string(static_cast<int>(*it)));
            }
        }
        format.append(u8"m");
        std::cout << format;
    }
public:
    static void Clear()
    {

#ifdef WINDOWS_PLATFORM
        std::system(u8"cls");
#elif LINUX_PLATFORM || defined MACOS_PLATFORM
        std::system(u8"clear");
#elif EMSCRIPTEN_PLATFORM
        emscripten::val::global()["console"].call<void>(u8"clear");
#else
        static_assert(false, "Unknown Platform");
#endif
    }

    static void Write(const String& s, ConsoleForeground foreground = ConsoleForeground::DEFAULT, ConsoleBackground background = ConsoleBackground::DEFAULT, std::set<ConsoleTextStyle> styles = {})
    {
#ifndef EMSCRIPTEN_PLATFORM
        EnableVirtualTermimalProcessing();
        SetVirtualTerminalFormat(foreground, background, styles);
#endif
        String str = s;
#ifdef WINDOWS_PLATFORM
        WString unicode = Strings::StringToWideString(str);
        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), unicode.c_str(), static_cast<DWORD>(unicode.length()), nullptr, nullptr);
#elif defined LINUX_PLATFORM || defined MACOS_PLATFORM || EMSCRIPTEN_PLATFORM
        std::cout << str;
#else
        static_assert(false, "Unknown Platform");
#endif

#ifndef EMSCRIPTEN_PLATFORM
        ResetTerminalFormat();
#endif
    }

    static void WriteLine(const String& s, ConsoleForeground foreground = ConsoleForeground::DEFAULT, ConsoleBackground background = ConsoleBackground::DEFAULT, std::set<ConsoleTextStyle> styles = {})
    {
        Write(s, foreground, background, styles);
        std::cout << std::endl;
    }

    static void Write(const WString& s, ConsoleForeground foreground = ConsoleForeground::DEFAULT, ConsoleBackground background = ConsoleBackground::DEFAULT, std::set<ConsoleTextStyle> styles = {})
    {
#ifndef EMSCRIPTEN_PLATFORM
        EnableVirtualTermimalProcessing();
        SetVirtualTerminalFormat(foreground, background, styles);
#endif
        WString str = s;

#ifdef WINDOWS_PLATFORM
        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), str.c_str(), static_cast<DWORD>(str.length()), nullptr, nullptr);
#elif LINUX_PLATFORM || MACOS_PLATFORM || EMSCRIPTEN_PLATFORM
        std::cout << Strings::WideStringToString(str);
#else
        static_assert(false, "Unknown Platform");
#endif

#ifndef EMSCRIPTEN_PLATFORM
        ResetTerminalFormat();
#endif
    }

    static void WriteLine(const WString& s, ConsoleForeground foreground = ConsoleForeground::DEFAULT, ConsoleBackground background = ConsoleBackground::DEFAULT, std::set<ConsoleTextStyle> styles = {})
    {
        Write(s, foreground, background, styles);
        std::cout << std::endl;
    }

    static void WriteLine()
    {
        std::cout << std::endl;
    }

    static void Pause()
    {
        char c;
        do
        {
            c = getchar();
            std::cout << "Press Key " << std::endl;
        } while (c != 64);
        std::cout << "KeyPressed" << std::endl;
    }

    static int PauseAny(bool printWhenPressed = false, ConsoleForeground foreground = ConsoleForeground::DEFAULT, ConsoleBackground background = ConsoleBackground::DEFAULT, std::set<ConsoleTextStyle> styles = {})
    {
        int ch;
#ifdef WINDOWS_PLATFORM
        ch = _getch();
#elif LINUX_PLATFORM || MACOS_PLATFORM || EMSCRIPTEN_PLATFORM
        struct termios oldt, newt;
        tcgetattr(STDIN_FILENO, &oldt);
        newt = oldt;
        newt.c_lflag &= ~(ICANON | ECHO);
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);
        ch = getchar();
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
#else
        static_assert(false, "Unknown Platform");
#endif
        if (printWhenPressed)
        {
            Console::Write(String(1, ch), foreground, background, styles);
        }
        return ch;
    }
};



int main()
{

    //u8"?" 4 char units

    char charPadding = u8'@';
    std::string stringPadding = u8"<>"s;
    std::string unicodeStringPadding = u8"?"s;
    
    std::locale::global(std::locale(u8"en_US.UTF8"));

    //No UTF8 examples
    String str = u8"Hello ?"s;
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadLeft(str, 10, charPadding) + u8" █ PadLeft with char padding");//"Hello ?" No changes. Length 10 == totalWith 10
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadLeft(str, 15, charPadding) + u8" █ PadLeft with char padding");//"@@@@@Hello ?"

    str = u8"Hello ?"s;
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadRight(str, 10, charPadding) + u8" █ PadRight with char padding");//"Hello ?" No changes. Length 10 == totalWith 10
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadRight(str, 15, charPadding) + u8" █ PadRight with char padding");//"Hello ?@@@@@"

    str = u8"Hello ?"s;
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadLeft(str, 10, stringPadding) + u8" █ PadLeft with string padding");//"Hello ?" No changes. Length 10 == totalWith 10
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadLeft(str, 15, stringPadding) + u8" █ PadLeft with string padding");//"<<><>Hello ?"

    str = u8"Hello ?"s;
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadRight(str, 10, stringPadding) + u8" █ PadRight with string padding");//"Hello ?" No changes. Length 10 == totalWith 10
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadRight(str, 15, stringPadding) + u8" █ PadRight with string padding");//"Hello ?<><><"

    //UTF8 Examples
    str = u8"Hello ?"s;
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadLeftUTF8(str, 7, charPadding) + u8" █ PadLeftUTF8 with char padding");//"Hello ?" No changes. UTF8Length 7 == totalWith 7
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadLeftUTF8(str, 15, charPadding) + u8" █ PadLeftUTF8 with char padding");//"@@@@@@@@Hello ?"

    str = u8"Hello ?"s;
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadRightUTF8(str, 7, charPadding) + u8" █ PadRightUTF8 with char padding");//"Hello ?" No changes. UTF8Length 7 == totalWith 7
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadRightUTF8(str, 15, charPadding) + u8" █ PadRightUTF8 with char padding");//"Hello ?@@@@@@@@"

    str = u8"Hello ?"s;
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadLeftUTF8(str, 7, unicodeStringPadding) + u8" █ PadLeftUTF8 with string padding");//"Hello ?" No changes. UTF8Length 7 == totalWith 7
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadLeftUTF8(str, 15, unicodeStringPadding) + u8" █ PadLeftUTF8 with string padding");//"????????Hello ?"

    str = u8"Hello ?"s;
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadRightUTF8(str, 7, unicodeStringPadding) + u8" █ PadRightUTF8 with string padding");//"Hello ?" No changes. UTF8Length 7 == totalWith 7
    Console::WriteLine(u8"Original: " + str + u8" █ StrLength: " + std::to_string(str.length()) + u8" █ UTF8Chars: " + std::to_string(Strings::LengthUTF8(str)) + u8" █ Padded:" + Strings::PadRightUTF8(str, 15, unicodeStringPadding) + u8" █ PadRightUTF8 with string padding");//"Hello ?????????"

    Console::WriteLine();
    Console::WriteLine(u8"Press any key to exit"s, ConsoleForeground::DARK_GRAY);
    Console::PauseAny();

}

输出

Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ? █ PadLeft with char padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:@@@@@Hello ? █ PadLeft with char padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ? █ PadRight with char padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ?@@@@@ █ PadRight with char padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ? █ PadLeft with string padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:<<><>Hello ? █ PadLeft with string padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ? █ PadRight with string padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ?<><>< █ PadRight with string padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ? █ PadLeftUTF8 with char padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:@@@@@@@@Hello ? █ PadLeftUTF8 with char padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ? █ PadRightUTF8 with char padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ?@@@@@@@@ █ PadRightUTF8 with char padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ? █ PadLeftUTF8 with string padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:????????Hello ? █ PadLeftUTF8 with string padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ? █ PadRightUTF8 with string padding
Original: Hello ? █ StrLength: 10 █ UTF8Chars: 7 █ Padded:Hello ????????? █ PadRightUTF8 with string padding

使用新的 Windows Terminal 可以绘制 unicode glyps-emoticons。在 Visual Studio + /std:c++17 + /utf-8 中使用 Microsoft Visual C++ 编译器:

Windows Terminal, windows, visual studio, msvc

使用经典控制台 cmd.exe 绘图效果不佳

cmd, windows, visual studio, msvc

在 repl.it 中输出,使用 Linux + Clang + std=c++17 https://repl.it/@JomaCorpFX/Padding#main.cpp

Linux, clang

答案 12 :(得分:-1)

这个怎么样:

const int column_len = 50;
string s;
s = "....some value ... ";
int fill_pad = column_len - s.length();
std::cout << s << std::setw(fill_pad) << " Second column " << other_field << std::endl;

答案 13 :(得分:-3)

创建一个包含10个空格的新字符串,并在两个字符串中向后工作。

string padstring(const string &source, size_t totalLength, char padChar)
{
    if (source.length() >= totalLength) 
        return source;

    string padded(totalLength, padChar);
    string::const_reverse_iterator iSource = source.rbegin();
    string::reverse_iterator iPadded = padded.rbegin();
    for (;iSource != source.rend(); ++iSource, ++iPadded)
        *iPadded = *iSource;
    return padded;
}