D
D
Detcelfer2018-12-14 04:12:49
Arduino
Detcelfer, 2018-12-14 04:12:49

How to put the value of a variable inside a string?

There is a library with a method that takes a string as an argument:

String timeNow = time.gettime("h:m:s");
// возвращает строку со значением времени как в указанной маске, к примеру "03:57:29"

I need to pass a string variable as a method argument instead of a string, but this doesn't work:
String var = "h:m:s";
String timeNow = time.gettime(var);

This way doesn't work either:
String var = "h:m:s";
String timeNow = time.gettime("" + var + "");

What am I doing wrong? What are the ways to solve such a problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Detcelfer, 2018-12-15
@detcelfer

Thank you Vladimir

String var = "h:m:s";
String timeNow = time.gettime(var.c_str());

V
vanyamba-electronics, 2018-12-14
@vanyamba-electronics

const char timeFormat[] = "h:m:s";
String timeNow = time.gettime(timeFormat);

String timeFormat = "h:m:s";
String timeNow = time.gettime((const char*) timeFormat);

Look in the String class for a method that returns a C-string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question