-3

Possible Duplicate:
C# string formatting and padding

In my C# program, I have four variables:

string checkNo = "1234";
string checkAmount = "25.60";
string checkCashedDate = "120413";
string filler = "   "; // three spaces

I'd like to build a string (26 positions long) that looks like this:

"00123400000002560120413   "

I can manually do, i.e. check each fields length (remove decimals), pad with the appropriate amount of zeros, append to final string. But, I was hoping that there was a quicker way. Like creating a string with a certain format (all zeros) and then "dropping in" the values. Is there anything like this available in C#? Thanks for any help and advice.

5
  • 7
    Come on, you're not even trying. Use string.Format. blog.stevex.net/string-formatting-in-csharp Commented May 15, 2012 at 16:06
  • I would recommend using the FileHelpers library instead, just guessing. Commented May 15, 2012 at 16:07
  • The string formatting site you provided is awesome! Thanks so much! Commented May 15, 2012 at 16:14
  • I have to downvote this question. This question shows no effort to solve the problem yourself. Commented May 15, 2012 at 16:24
  • Sorry about that, Ramhound. I've never used any type of string manipulation in C# and figured I could ask the programmers here to point me in the right direction. The string formatting site that Thinking Sites listed earlier was a big help! Commented May 15, 2012 at 16:26

4 Answers 4

5

You can use String.PadLeft to build this string appropriately:

string result = checkNo.PadLeft(6, '0') + 
                checkAmount.Replace(".","").PadLeft(11, '0') +
                checkCashedDate.PadLeft(6, '0') + filler;

That being said, I would personally store these values as numerical values and a DateTime instead of string values, and use a single String.Format statement to reformat the results. This provides more type safety while working with the values.

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

2 Comments

Thanks, Reed! The reason those variables are in string format is because I'm reading them in from a text file and parsing those values out of the resulting input. Should I, instead of keeping them as strings, do as you suggest and store them as their appropriate data types? I'm basically just reading from one text file, creating a string from the input, and writing it out to another text file to be sent on to a banking entity.
@Kevin It depends - it's often safer to treat them in their appropriate types, especially if there is any variation in formatting...
4

Use string.PadLeft to ensure the width of each part of the larger string, in conjunction with string.Format and string.Replace.

checkNo.PadLeft(6, '0') + 
checkAmount.Replace(".", "").PadLeft(11, '0')  + 
checkCashedDate.PadLeft(6, '0') + filler

Or:

string.Format("{0}{1}{2}{3}",
              checkNo.PadLeft(6, '0'),
              checkAmount.Replace(".", "").PadLeft(11, '0'),
              checkCashedDate.PadLeft(6, '0'),
              filler);

Or, (using Composite Formatting), as Guffa posted:

string.Format(
  "{0,6}{1,11}{2,6}",
  checkNo,
  checkAmount.Replace(".", ""),
  checkCashedDate
).Replace(" ", "0") + filler

2 Comments

Shouldn't the checkCashedDate padding be PadLeft(6, ...? (The 0 is from 25.60 in the previous element.)
@ReedCopsey - Yes, it should. All those 0 have confused my eyes...
2

You can use String.Format to pad with spaces, and replace those spaces with zeroes:

String.Format(
  "{0,6}{1,11}{2,6}",
  checkNo,
  checkAmount.Replace(".", ""),
  checkCashedDate
).Replace(" ", "0") + filler

Comments

1

Check out the String.Format() method.

This example shows how to use the String.Format method to generate the string of numbers:

String.Format("{0,6}{1,11}{2,6}", checkNo, checkAmount, checkCashedDate).Replace(' ','0');

4 Comments

Not clear how this is going to help, completely, as the OP is working from string variables....
You will also need to convert the strings to floating point variables first
Can we get more then just a single link to how one of the most basic methods in C# works? If the author knew how to use this method he would have.
@Ramhouse Done before you asked =)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.