I have two strings I want to combine and have not exceed a certain width.
$string = sprintf("%4(%s %s)", $s1, $s2);
//s1 s
Notice how the combined %s %s did not exceed a width of 4.
How can I do this with sprintf?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You probably need to add more detail to your question – what do you want to happen if the fields are longer than 3 characters (4 – one space)?
But maybe this is what you want:
That will output the combined string
"$s1 $s2", pruned to 4 characters. Of course substr would do the same, this might be useful in the context of a longer format string.To be honest I hadn’t realised before that the precision specifier (
.followed by number) could be used with%s.