String possible with format or any workaroud?

Hi guys,

I am playing around with the String command and was wondering if there is a chance to give it a format.

Basically I have to concatenate strings (str_n) but it would like to have str_4 at least 2 characters wide. For example:

str_5 = 0, number = 8 therefore str_4 should be 08
str_5 = 1, number = 9 therefore str_4 should be 09
str_5 = 2, number = 10 therefore str_4 should be 0A
and so on...

I don't want to bore you but background is that I am having a LabVIEW program that is doing some LIN communication and that I have to transfer that to an Arduino Mega.

I think that I read that the String class is not optimal but in my case it offers quite a few very helpful features, except this one... As well I have to admit that I am fairly new to C programming.

Kind regards,

void setup()
{
   Serial.begin(9600);
}

void loop()
{
  delay (500);
  LIN_Master(1,"?","");
}



void LIN_Master(byte out_adr, String out_cmd, String out_data)
{
  String str_1 = "@";
  String str_2 = String(out_adr);
  String str_3 = out_cmd.substring(0,1);
  String c = "?";
  boolean bool_1;
  String str_4="";
  String str_5="";
  
  int number;
  

  if (str_3 == c)
  {
    bool_1 = true;
    str_5="";
    number = 8 + str_5.length();
   
    str_4 = String(number, HEX);

    
  }
  else
  {
    bool_1 = false;
    str_5 = out_data;
    number = 8 + str_5.length();
    
    str_4 = String(number, HEX);
  }    
 

Serial.print(str_1+str_2+str_3+str_4+str_5);Serial.print('\n');        

}

I am playing around with the String command

Stop right now.

but in my case it offers quite a few very helpful features

ALL of which are based on standard string capabilities. EVERY SINGLE ONE OF THEM.

sprintf() will allow you to do formatted strings easily.

If you insist on using Strings, YOU must do the formatting.