1

I am looking for some generic solution, so that I can generate the string. Like I want first 10 characters to be reserved for id, next 20 characters needs to reserved for name etc. but this character length my change. This requirement is for ibm mq message,. E.g.: if my id is 001, name is namexxxx and age 25 then my final string should look like:

001       namexxxx            25
0

1 Answer 1

4

Using String.PadRight you can achieve that. You can also pass these as parameters to a method, or read them from a configuration file or database:

Console.WriteLine(id.PadRight(10) + name.PadRight(20) + age.PadRight(5));

Another way would be to use String.Format but this one is a bit messier to parametrize:

Console.WriteLine(String.Format("{0,-10}{1,-20}{2,-5}", id, name, age));

Output in both cases:

001       namexxxx            25   
Sign up to request clarification or add additional context in comments.

1 Comment

@user1739969 Please mark this answer as accepted if you find it helpful. If you don't know how, it is a simple single step described here: meta.stackexchange.com/a/5235

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.