You can store your string in two variables font_size
and html_template
and use the sprintf
function to "compile" your template with the values you want as follow :
char* html_template = "<font color='#00ff00' size='%d'> SampleText </font>";
int font_size = superFontSizeCalculation();
char* compiled_html = malloc(strlen(html_template));
sprintf(compiled_html, html_template, font_size);
I don't check any return of the str* functions but in real usage, you should do it.
Edit : As you changed your mind and want know java and not C, you can have the same mechanism with a small template engine by putting tags in your template string and compile by replacing the tags by your values.
For example :
static final String FONT_SIZE_TAG = "<FONT_SIZE_TAG>";
static final String FONT_COLOR_TAG = "<FONT_COLOR_TAG>";
String htmlString ="<font color='" + FONT_COLOR_TAG + "' size='" + FONT_SIZE_TAG + "'> SampleText </font>";
String compiled = htmlString.replace(FONT_COLOR_TAG, "#00ff00").replace(FONT_SIZE_TAG, String.valueOf(FONT_COLOR_TAG));