fill with "0"

Hi, I'm a beginner and I have a question. I have a String variable a = "1001" and I need to fill in "0" on the left until 9 positions are complete. Does anyone know a more "sophisticated" way to accomplish this?

Thanks in advance.

More sophisticated than what?

// No, I don't know why nine places, but hey, not my 
// project
void printNinePlacesBinary (uint8_t val)
{
  Serial.print (&"00000000"[(val == 0) ? 0 : ((sizeof (int) * 8) - 1) - __builtin_clz (val)]);
  Serial.print (val, BIN);
}

Does anyone know a more "sophisticated" way to accomplish this?

More sophisticated than what ?

Use the following function:

Or, if you’re using c-strings as recommended...

Declare your char array with

char mystring[] = “123456789”;

// any time you want to zero out the array, use...
memset(&mystring, 0, sizeof(mystring));

— which fills mystring with zeroes
Remember to leave space for the trailing null

Don't use c-string methods, stick with Strings, they are safer, no need to remember trailing null and stuff
This code does what you want.

String a = "1001";

void padStringTo9(String &str) {
  str.reserve(9); // should be done earlier, but if not do here
  while (str.length() < 9) {
    str = "0" + str;
  }
}

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    delay(500);
    Serial.print(i); Serial.print(' ');
  }
  Serial.println();
  a.reserve(9);  // reserve space to prevent fragmentation
  padStringTo9(a);
  Serial.println(a);
}

void loop() {
}

Don't use Strings - too much baggage.

It seems to me that we don't actually know what the OP wants to do

I have a String variable a = "1001" and I need to fill in "0" on the left until 9 positions are complete

It could be that they want to create a String such as "0000000001001"

drmpf:
Don't use c-string methods, stick with Strings, they are safer,

The String class is most certainly NOT safer in the small memory of an Arduino. It may appear easier to use (which is why it was created) but that ease of use can lead to memory corruption in the small memory of an Arduino.

Use c-strings.

...R

Please guys, the OP hasn't even responded.

Deva_Rishi:
Please guys, the OP hasn't even responded.

Don't blame us !

He posted his question 24 hours ago and has been online today

that ease of use can lead to memory corruption in the small memory of an Arduino.
Use c-strings

Over three decades of experience has shown use of c-strings leads to programming errors. Strings are safer and in the solution code above does result in fragmentation.

Over three decades of experience has shown use of c-strings leads to programming errors.

... if you don't know what you're doing

Strings are safer and in the solution code above does result in fragmentation.

... if you know what you're doing

... if you know what you're doing

You can code in asm or byte code or c-strings, if you know what you are doing, but it is just so much easier to stuff up doing that. Strings was introduced in C++ to over come the systemic problems of coding errors using c-strings.

drmpf:
Strings was introduced in C++ to over come the systemic problems of coding errors using c-strings.

...as the typical available memory followed Moore's law on the vast majority of target systems... not so much in the embedded world.

not so much in the embedded world.

No true, just compare the SRAM of an ATtiny13 (64bytes) to UNO (2048) to Nano 33 (256K) and higher. Arduino CC decided UNO was suitable for supporting String and modern single chip micros have so much more SRAM available.

No, Arduino CC undoubtedly used String in many examples, because it is easier for a beginner to understand. The newer processors weren't available back when the majority of those example sketches were written. Many processors still have a relatively small memory because that's all they really need to perform some embedded task. In such cases, the advantage is reduced power consumption and cost - sometimes a lot of die space goes into on-chip peripherals like timers, etc.

drmpf:
Don't use c-string methods, stick with Strings, they are safer, no need to remember trailing null and stuff

Ahhh, the "make the computer do it FOR me and save me the drudgery of thinking" mentality, even if it takes 8 times the memory and resources.

make the computer do it FOR me

absolutely that is what compilers and supporting classes like Serial and String do.

Arduino CC undoubtedly used String in many examples, because it is easier for a beginner to understand

and safer to program with. The sketch above works what is your actual complaint in this case?

But how do you put a memory manager and "garbage collector" in 2k of memory and have room left for anything more complicated than an LED blinker?
The above program has 1 String, of course it works.