text and variable both in display.println()?

Newbe here with a quick question, I know I can print text on an oled display with

display.println("hi this is my text...")

and a print a variable like this

display.println(myText)

But is it possible to do both in one??? something like

display.println("text here",myText)

is it possible to do both in one???

You could use snprintf() to concatenate the text and value in a text buffer then print that or if the second value is a string (lowercase s) concatenate the two using strcat() but why bother ?

I was thinking it would make coding much neater and smaller and was more currious if it was or wasn't possible. So just leave it?

display.println("text here")
display.println(myText1)
display.println("some more text")
display.println(mytext2)
display.println("and more text")

If you dont want the linebreak, do print instead of println.

But yeah - dont build a string then print it in arduino, if you can avoid it - just use successive prints. Building thf string is more work, and requires more memory because you need to store the string you are building. What is good practice when you have gigabytes of ram isnt so great when you have kilobytes of ram.

You can also use the >> operator:

template <typename T>
Print& operator<<(Print& printer, T value)
{
    printer.print(value);
    return printer;
}

void setup()
{
    Serial.begin(9600);
    int num = 123456;
    Serial << "My variable's number is: " << num << '\n';
}

I was thinking it would make coding much neater and smaller and was more currious if it was or wasn't possible. So just leave it?

If you want it to look neater you could hide the multiple print lines in a function. Something like this

void multiPrint(char * s1, char * s2 = "", char * s3 = "")  //note default parameters
{
  Serial.print(s1);
  Serial.print(s2);
  Serial.print(s3);
}

void setup()
{
  Serial.begin(115200);
  multiPrint("Hello ", "World\n");
  multiPrint("A single string\n");
  multiPrint("1 ", "2 ", "3 \n");
}

void loop()
{
}

Note that the IDE fails to automatically build a function prototype for functions with default parameters so the function definition has to precede where it is called

If you really want to keep the function call instead of the stream operator, I’d suggest going the variadic template route:

// Called last from the variadic template function
void printLine()
{
  Serial.println();
}

template <typename T, typename... Types>
void printLine(T first, Types... other)
{
  Serial.print(first);
  printLine(other...) ;
}

These two template functions allow you to write any combination of parameters you like:

printLine("Hello ", "World");
printLine("My number is: ", 123456);
printLine("X: ", 10, " Y: ", 20);

EDIT: For your OLED display just change every “Serial” with “display”, where display is the instance of your OLED display class.