text two value wtih string.format

hi,

i don’t see the issue with this. i have follow this tutorial

https://coronalabs.com/blog/2015/02/17/tutorial-formatting-values-using-string-format/

i want that my textScore.text=level n°1

where is my error ?

numberlevel=lvl[cnt.lvl].text --lvl[cnt.lvl].text=1 print("numberlevel",numberlevel) --result=1 textScore.text=string.format( "level n°", numberlevel ) print(textScore.text) --result : level n° or i want this : level n°1

You just need to insert a placeholder “%s” in the string passed to string.format (I am suggesting %s because numberLevel is a string. For a number you might use %d as described in Rob’s tutorial.

textScore.text=string.format( “level n°%s”, numberlevel )

now the value of your numberLevel variable will take the place of %s in the final string

textScore.text=string.format( "level %d", numberlevel )

or

textScore.text=string.format( "level n°%d", numberlevel )

I wasn’t clear if you meant you wanted ‘n°’ in front of then number or replaced by it.

By the way, there is a reference for this:

https://docs.coronalabs.com/daily/api/library/string/format.html

string.format() follows the same rules as the C-language printf():

http://www.cplusplus.com/reference/cstdio/printf/

For example, if you wanted your level number to always have 6 digits left-padded with zeroes you’d do this:

textScore.text=string.format( "level %06.6d", numberlevel )

See the printf() reference to understand what I did there:

format specifier follows this prototype: [see compatibility note below
%[flags][width][.precision][length]specifier 

          flag 0 (pad left with 0s)

       width 6 (max length 6)

  precision 6 (min  length 6) 

specifider d (print an integer)

espace3d, why don’t you use concatenation?

textScore.text="level nº "..numberlevel

Hello everybody,

Thanks for your advice and your solutions  :slight_smile:

For a question of facility i prefer the solution of Carlacosta.

I never liked the “%”…in bash (linux) I 've never done well with this character especially with the command “find”  :wacko:

You just need to insert a placeholder “%s” in the string passed to string.format (I am suggesting %s because numberLevel is a string. For a number you might use %d as described in Rob’s tutorial.

textScore.text=string.format( “level n°%s”, numberlevel )

now the value of your numberLevel variable will take the place of %s in the final string

textScore.text=string.format( "level %d", numberlevel )

or

textScore.text=string.format( "level n°%d", numberlevel )

I wasn’t clear if you meant you wanted ‘n°’ in front of then number or replaced by it.

By the way, there is a reference for this:

https://docs.coronalabs.com/daily/api/library/string/format.html

string.format() follows the same rules as the C-language printf():

http://www.cplusplus.com/reference/cstdio/printf/

For example, if you wanted your level number to always have 6 digits left-padded with zeroes you’d do this:

textScore.text=string.format( "level %06.6d", numberlevel )

See the printf() reference to understand what I did there:

format specifier follows this prototype: [see compatibility note below
%[flags][width][.precision][length]specifier 

          flag 0 (pad left with 0s)

       width 6 (max length 6)

  precision 6 (min  length 6) 

specifider d (print an integer)

espace3d, why don’t you use concatenation?

textScore.text="level nº "..numberlevel

Hello everybody,

Thanks for your advice and your solutions  :slight_smile:

For a question of facility i prefer the solution of Carlacosta.

I never liked the “%”…in bash (linux) I 've never done well with this character especially with the command “find”  :wacko: