Matt said:
string format formula to write a text file
below is not working for me
pos 1 to 10 name
pos 15 to 30 lastname
pos 45 to 75 job
Adam smith programmer
string fmt = "{1,10}{15-30}{45,75}" // is not working any help ?
sb.Append(string.Format(fmt,"Adam","smith","programmer"));
Hi Matt,
James has already given you the basic solution.
string.Format ("{0,-10}....{1,-15}...............{2,-30}|", "Adam","Smith","Programmer");
HOWEVER
If any of the columns can exceed it's assigned width, this will fail.
Here is a method that works, but it is a tad icky.
You need to use construct like this
string name = InputString.PadRight(15).Substring(0,15);
It would be nice to just be able to use Substring, but that throws an exception if the string is
too short.
Try this to see the concept in action:
for(int i = 1; i < 45; i++)
{
string a = new string('a',i).PadRight(15,':').Substring(0,15);
string b = new string('b',i).PadRight(15,':').Substring(0,15);
string c = new string('c',i).PadRight(30,':').Substring(0,30);
string str = string.Format("{0,-10}.....{1,-15}...............{2,-30}",a,b,c);
Console.WriteLine("{0}",str);
}
Bill