String Format help

M

Matt

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"));
 
K

Kevin Spencer

Hi Matt,

To start with, you're using the String.Format function all wrong. Take a
look at the following:

http://msdn.microsoft.com/library/d...ef/html/frlrfsystemstringclassformattopic.asp

You can only pass one string to the function.

Assuming that you're not working with literal strings, you should be able to
use the String.PadRight() method to build you string. Example:

string firstName = "Adam";
string lastName = "Smith";
string job = "programmer";

string together = firstName.PadRight(10) + lastName.PadRight(30) +
job.PadRight(75);

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
J

James Curran

This worked for me:


Console.WriteLine("123456789a123456789b123456789c123456789d123456789e1234567
89f123456789g123456");
Console.WriteLine("{0,-10}....{1,-15}...............{2,-30}|", "Adam",
"Smith","Programmer");


In each {n,m}, the first number is the position of the parameter, the second
number is the length of the field, negative meaning padded on the right
(positive numbers would pad on the left).

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
M

Matt

Thanks all
Anyway to option explicit this formula
what i mean with that is
Console.WriteLine("{0,-10}....{1,-15}...............{2,-30}|", "Adam",
"Smith","Programmer");
I like name( in this case Adam 4 char )
what happens if the name is more that 4 char like (GordonGongolaz)
I am happy if it does not push others and just put GordonGong
 
B

Bill Butler

Kevin Spencer said:
Hi Matt,

To start with, you're using the String.Format function all wrong. Take a look at the following:

http://msdn.microsoft.com/library/d...ef/html/frlrfsystemstringclassformattopic.asp

You can only pass one string to the function.
<snip>

I am Soooooooo confused!?!?
The link you posted CLEARLY shows multiple overloads for string.Format()

His problem was a badly structured format string. But James has already explained that.
Assuming that you're not working with literal strings, you should be able to use the
String.PadRight() method to build you string. Example:

string firstName = "Adam";
string lastName = "Smith";
string job = "programmer";

string together = firstName.PadRight(10) + lastName.PadRight(30) +
job.PadRight(75);

Uhhhh. That doesn't work either.
job.PadRight(75);
will create string with a minimum Length of 75, when he wanted a string spanning column 45 to 75

Bill
 
K

Kevin Spencer

I stand corrected. D**n, I'm working too hard these days. Thanks for
pointing that out!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

James Curran said:
You can only pass one string to the function.

Nonsense. You can pass as many parameter as you like to String.Format,
and any of them can be strings.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
K

Kevin Spencer

Yeah, sorry. Ignore my last post.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
M

Matt

here is what i have
string fmt = "{0,-4}{1,-5}{2,-6}{3,-36}{4,-37}";
//start 0 to 4 write Adam // if the name is more than 4 just take 4
char dont pust other letters
//start 4 to 5 space
//start 6 to 35 LastName
//start 36 to 37 space
//start 38 to 75 job

sb.Append(string.Format(fmt,"Adam"," ","Gongodgozales","
","CrapyProgrammer"));
sb.Append("\n");
myWriter.Writer(fileName,sb.ToString());

Note ! lastname fails
{2,-6} fails it starts from 10th space on text file any idea?
 
B

Bill Butler

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
 
M

Matt

string fmt = "{0,-10}....{1,-15}...............{2,-30}|";
sb.Append(string.Format(fmt,"Adam","Smith","Programmer"));
myWrite .Writer(fileName,sb.ToString());
gives me
15 45
Adam ....Smith ...............Programmer
|

when i give formula
string fmt = "{0,-10}{1,-15}{2,-30}|";
11 26
Adam Smith Programmer
i just need it on
11 30
Adam Smith Programmer
is that mean formula would fail if i put this in loop of names
 
M

Matt

gave up for today
since i am using string builder to build a string from dataset and lots
of data in there
i am kinda of stuck
gave up for a today. James solution slides the text left and right

Thanks anyway
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top