Link to home
Start Free TrialLog in
Avatar of Jonathan D.
Jonathan D.Flag for Israel

asked on

Appending a string to a new column using StringBuilder

Hi, I have faced a serious challenge which I've been struggling with for some time. I couldn't find a solution to my use-case and after hours of searching on Google I couldn't find anything useful in my case. 


I have two strings which are just a series of a single character (digit) in line, I would like to Append them to a StringBuilder but the second string to the next column instead of continuing alongside the same line as the previous string.


For example, let's say I have these two strings:


7
8
9
10
11

Open in new window


And this one also


7
7
7
7
7

Open in new window


I would like them to be aside each other instead of the same line, like this:


7  7
8  7
9  7
10 7
11 7

Open in new window


How can I accomplish this with StringBuilder? I am aware of the String helper method PadRight() but it just doesn't work well with my current design. If there's another approach I'd like to hear about it, thanks.

Avatar of Bill Prew
Bill Prew

It feels like you are starting from a list of strings, rather than a single string, is that correct?  And therefore wanting a list of strings as the result?

If not then I'm confused, since in your text you mention "I have two strings which are just a series of a single character (digit) in line", but then in you sample you show "10" and "11" as input data, which are two digits long.

Need to better understand exactly what you mean.


»bp
Avatar of Jonathan D.

ASKER

It feels like you are starting from a list of strings, rather than a single string, is that correct?

I'm creating the list of digits characters using a StringBuilder. A for loop which Appends a new line with the calculated character. At the end of the routine I extract it with ToString and an outer foreach loop which iterates over a List of classes with similar behaviour to extract more lists I want to Append into another StringBuilder but with padding, which means each line (series of characters) will have its own column.
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@dpearson That's true and I thought about that, but look what I found when I searched "horizontal stringbuilder" on Google. This is amazing, I'm going to try this and come back to edit this comment with results. Standby.
The link you posted is basically doing just what I described :)

The key is that there's an outer loop adding one element at a time to the original.

The only difference is that each time they are splitting up the string so far in order to do the addition of one element.

This will work, but if you try it for 10,000 items to be added, you'll find their solution will take a long time to run.  It would be much more efficient to just break up the initial string and add the items back in pairs.

But hey either approach is basically equivalent.
It would be much more efficient to just break up the initial string and add the items back in pairs.

This is also true. But the reason I need to use this extension method is because of my current design of the program. It will be the solution to my current design of the code. What happens is, I have three classes. Each one of them implements an interface which has a method called GetRow() that returns a string. The row is just a vertical list of characters (each line is a single character). So using polymorphism, i'm iterating over a list of implementations and inside the loop and calling the method GetRow() which is very easy to use with this extension method. If you can think of a better approach which will not break my current design, I'd love to hear.
OK that sounds like a perfectly good reason to go with the extension model.  You're constrained by the existing design.
@dpearson After a lot of frustration, I decided to change the implementation and go with your approach which is writing it char by char (row by row) horizontal instead of a vertical column. But I faced another challenge which broke a feature, what if I have a column with 34 lines and the others are with 36 lines?
You want the loop to be over the maximum length of the different columns and if a particular row doesn't have an entry for that column, you'll need to insert a space instead.

I solved this problem with this code snippet. Now i'm working on revising the code to work directly from memory instead of reading from the file system. Thanks anyways, I'll still accept your comment as solution as it helped me see the big picture better.