Does anyone know how I can add "0" to the left side of a string? If I get "1234" I'd like to make it "012345", if I get "1234" I'd like to make it "001234". Any suggestions?

Recommended Answers

All 7 Replies

string someString("1234");
someString.insert(0,"0");

Have you tried:

s = '0' + s;

That's assuming you're using a std::string object. For C-style strings I'd recommend not using them because it makes just about everything harder.

Or, using std::stringstream:

std::string toString(unsigned int i) {
  std::stringstream ss;
  ss << std::setfill('0') << std::setw(6) << i;
  return ss.str();
};

mike, I don't think I can hard code something like setw(6) because I have no way of knowing how large of a number the user might enter into the program. I am also unfamilar with stringstream or unsigned

Moschops I got the following error when I tried you code.

NumbersToStrings.cpp:34:13: error: request for member ‘insert’ in ‘strNumber’, which is of non-class type ‘char*’

char *strNumber = argv[1];

strNumber.insert(0,"0");

deceptikon, your method compiled just fine but didn't actually change the output of the number. If I entered 12345 it would still out put 12345 rather than 012345 which is what I'M trying to get.

char *strNumber = argv[1];

it's different from the object :

string strNumber = argv[1];

char *strNumber is an array of characters, whereas string strNumber is a string object.

try this instead:

#include <string>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
    string nrString (argv[1]);
    nrString = '0' + nrString;
    cout<<nrString<<endl;
    return 0;
}

deceptikon, your method compiled just fine but didn't actually change the output of the number. If I entered 12345 it would still out put 12345 rather than 012345 which is what I'M trying to get.

Let's make something abundantly clear: the type of string matters. Are you using a C++ string object? In that case, my solution works:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s = "12345";

    s = '0' + s;

    cout << "'" << s << "'" << '\n';
}

If you're using a C-style string, which is nothing more than an array of char terminated by '\0', it's less intuitive, more awkward, and easier to get wrong:

#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    char s[10] = "12345";

    // Shift everything to the right by 1 to make room at the front
    memmove(s + 1, s, strlen(s) + 1);

    // Prepend '0'
    s[0] = '0';

    cout << "'" << s << "'" << '\n';
}
commented: From all suggested approaches I like these the most. +13

NumbersToStrings.cpp:34:13: error: request for member ‘insert’ in ‘strNumber’, which is of non-class type ‘char*’

As Deceptikon and others state, you're not actually using a string. This is C++, and in C++ a string is an object of type string created as so:

string someStringObject;

or

std::string someStringObject;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.