C++ 将前导零添加到字符串,没有 (s)printf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6143824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:33:44  来源:igfitidea点击:

Add leading zero's to string, without (s)printf

c++

提问by Guus Geurkink

I want to add a variable of leading zero's to a string. I couldn't find anything on Google, without someone mentioning (s)printf, but I want to do this without (s)printf.

我想在字符串中添加一个前导零的变量。我在谷歌上找不到任何东西,没有人提到(s)printf,但我想在没有(s)printf的情况下做到这一点。

Does anybody of the readers know a way?

有读者知道方法吗?

采纳答案by Michael Burr

// assuming that `original_string` is of type `std:string`:

std::string dest = std::string( number_of_zeros, '0').append( original_string);

回答by Ruslan Guseinov

I can give this one-line solution if you want a field of n_zero zeros:

如果你想要一个 n_zero 零的字段,我可以给出这个单行解决方案:

  std::string new_string = std::string(n_zero - old_string.length(), '0') + old_string;

For example: old_string = "45"; n_zero = 4; new_string = "0045";

例如: old_string = "45"; n_zero = 4; new_string = "0045";

回答by Rob?

You could use std::string::insert, std::stringstreamwith stream manipulators, or Boost.Format:

您可以使用std::string::insert,std::stringstream流操纵器Boost.Format

#include <string>
#include <iostream>
#include <iomanip>
#include <boost/format.hpp>
#include <sstream>

int main() {
  std::string s("12");
  s.insert(0, 3, '0');
  std::cout << s << "\n";

  std::ostringstream ss;
  ss << std::setw(5) << std::setfill('0') << 12 << "\n";
  std::string s2(ss.str());
  std::cout << s2;

  boost::format fmt("%05d\n");
  fmt % 12;
  std::string s3 = fmt.str();
  std::cout << s3;
}

回答by Jerry Coffin

You could do something like:

你可以这样做:

std::cout << std::setw(5) << std::setfill('0') << 1;

This should print 00001.

这应该打印00001.

Note, however, that the fill-character is "sticky", so when you're done using zero-filling, you'll have to use std::cout << std::setfill(' ');to get the usual behavior again.

但是请注意,填充字符是“粘性的”,因此当您使用零填充完成后,您将不得不再次使用它std::cout << std::setfill(' ');来获得通常的行为。

回答by Ivan Strelets

This works well for me. You don't need to switch setfill back to ' ', as this a temporary stream.

这对我很有效。您不需要将 setfill 切换回 ' ',因为这是一个临时流。

std::string to_zero_lead(const int value, const unsigned precision)
{
     std::ostringstream oss;
     oss << std::setw(precision) << std::setfill('0') << value;
     return oss.str();
}

回答by Ozair Kafray

The C++ way of doing it is with setw, ios_base::widthand setfill

C++ 的方法是使用setwios_base::widthsetfill

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    const int a = 12;
    const int b = 6;

    cout << setw(width) << row * col;
    cout << endl;

    return 0;
}

回答by Seth Robertson

memcpy(target,'0',sizeof(target));
target[sizeof(target)-1] = 0;

Then stick whatever string you want zero prefixed at the end of the buffer.

然后在缓冲区的末尾粘贴您想要零前缀的任何字符串。

If it is an integer number, remember log_base10(number)+1(aka ln(number)/ln(10)+1) gives you the length of the number.

如果它是一个整数,请记住log_base10(number)+1(又名ln(number)/ln(10)+1)为您提供数字的长度。