Click here to Skip to main content
15,886,725 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have 3 strings and each string has a fixed length and I have to write it in a file with default length even when the length of the string is lesser than the actual length.


str1="1234";(default length is 4)
str2 = "56";(default length is 2)
str3 = "789";(default length is 3)

So, my string in file should look like "123456789" and when str1 = "123" my string should look like "123 56789".

So there should be a empty space when the length of the string is lesser than actual length.
Posted
Comments
F-ES Sitecore 12-Oct-15 5:58am    
Use an array of char rather than strings (you can convert your strings into char arrays to make it easier to build). If the unused elements are spaces then you can convert the arrays to strings when writing them to the file and they'll keep their lengths.

To pre-empt your next question, no I won't write the code for you. Google converting strings to arrays and vice versa to find sample code.

You could try the following

1. Create a class called FixedLengthString (for example).
In the class you have a constructor with a the length as a parameter.
Then you can implement an explicit conversion operator[^] (it works as an overload of the assignment operator) so it pads a string shorter than the fixed length and truncates a longer string or throws an error message.
You should also override the ToString method in order to trim the trailing space.
Implent a method void GetString(string fileContent, int index) that copies a sub-string from a specific index.

2. Create a container class that has a list of FixedLengthString, List<FixedLengthString>, as a member variable and a Serialize and Deserialize method.
In the serialize method you loop through your list and append the strings to a StringBuilder. Then write the content of the StringBuilder variable to the file.
In the Deserialize method you read the file into a string and then you loop through the list and use the GetString method.
Update the index with the length of the current list member for every iteration.

Don't forget to add error handling if the file content is to short etc.

This will save you a lot of hassle if the number of strings are variable or will be increased over time.
 
Share this answer
 
v3
This is simple to accomplish using the alignment option[^] in a format string:
C#
string str1 = "123";
string str2 = "4";
string str3 = "5";
string result = string.Format("{0,-4}{1,-2}{2,-3}", str1, str2, str3);
// result == "123 4 5  "
 
Share this answer
 

Try doing something like this.


C#
List<string>stringData= new List<string>{"1234","56","789"};
           int[] dataLength =  { 4, 4, 3 };
           string spaces = "                          ";
           StringBuilder sb= new StringBuilder();
           int i = 0;
           foreach (var s in stringData)
           {
               sb.Append(s).Append(spaces.Substring(0, dataLength[i] - s.Length));
               i++;
           }
           Console.WriteLine(sb);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900