producing strings of various formats

  • Thread starter Thread starter Beenish Sahar Khan
  • Start date Start date
B

Beenish Sahar Khan

I have a string in following format ,
XXdd XXX

Where X exists b/w. A - z
d exists b/w. 0 - 9

I want to format them in the following manner,
xxdd xxx
xx dd xxx
xxd dxxx
xx ddxxx
xxddx xx
x xddxxx
x xdd xxx
xxd d xxx
xxdd xx x
xxdd x xx
xxx dd xx x
xxx ddx x x

is there any simple and easy way through which i can convert a single string
into multiple strings of my desired format.

regards,
Beenish Khan
 
Is there some speedy .NET Framework method you can call? No.

In my opinion, the best way to code this so that it can be easily
understood and maintained is as follows.

I suppose that each part of your original string has some meaning. That
is, they're not just seven random characters tossed together. It looks
to me as though you have groups of characters that mean things. So, for
example, perhaps the two x's before the d's represent some code for
something, the digits represent something else, etc. I would pick the
original string apart and put the resulting bits into strings that
describe what they mean.

To take another example, imagine that you know that the string you're
receiving contains the location of a seat in a stadium. It consists of
a letter indicating the level (lower or upper), a code indicating the
section, a row number, and a seat code. You could then pick the string
apart like this:

string levelCode = seatingLocation.Substring(0,1);
string sectionCode = seatingLocation.Substring(1, 2);
string rowNumber = seatingLocation.Substring(3, 3);
string seatNumber = seatingLocation.Substring(6, 2);

Now you can format the string however you like:

string printingSeatingLocation = String.Format("{0}{1}/{2}/{3}",
levelCode, sectionCode, rowNumber, seatNumber);

etc.

Is this the fastest way to do it, CPU-wise? Probably not. However,
three years from now you'll be able to come back, read the code again,
and understand exactly what you did and why.
 
Back
Top