473,587 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Format help

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(strin g.Format(fmt,"A dam","smith","p rogrammer"));

Nov 17 '05 #1
11 2115
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/de...ormattopic.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.PadRi ght(10) + lastName.PadRig ht(30) +
job.PadRight(75 );

--
HTH,

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

"Matt" <me*******@Hotm ail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
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(strin g.Format(fmt,"A dam","smith","p rogrammer"));

Nov 17 '05 #2
This worked for me:
Console.WriteLi ne("123456789a1 23456789b123456 789c123456789d1 23456789e123456 7
89f123456789g12 3456");
Console.WriteLi ne("{0,-10}....{1,-15}............ ...{2,-30}|", "Adam",
"Smith","Progra mmer");
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
"Matt" <me*******@Hotm ail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
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(strin g.Format(fmt,"A dam","smith","p rogrammer"));

Nov 17 '05 #3
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:#D******** *****@TK2MSFTNG P10.phx.gbl...
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
Nov 17 '05 #4
Thanks all
Anyway to option explicit this formula
what i mean with that is
Console.WriteLi ne("{0,-10}....{1,-15}............ ...{2,-30}|", "Adam",
"Smith","Progra mmer");
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

Nov 17 '05 #5
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
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/de...ormattopic.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.PadRi ght(10) + lastName.PadRig ht(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


Nov 17 '05 #6
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(strin g.Format(fmt,"A dam"," ","Gongodgozale s","
","CrapyProgram mer"));
sb.Append("\n") ;
myWriter.Writer (fileName,sb.To String());

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

Nov 17 '05 #7
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" <ja*********@mv ps.org> wrote in message
news:uR******** ******@tk2msftn gp13.phx.gbl...
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:#D******** *****@TK2MSFTNG P10.phx.gbl...
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

Nov 17 '05 #8
Yeah, sorry. Ignore my last post.

--
HTH,

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

"Bill Butler" <qw****@asdf.co m> wrote in message
news:GdOaf.89$4 1.17@trndny04.. .
"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
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/de...ormattopic.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.PadRi ght(10) + lastName.PadRig ht(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



Nov 17 '05 #9

"Matt" <me*******@Hotm ail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
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(strin g.Format(fmt,"A dam","smith","p rogrammer"));


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.Pad Right(15).Subst ring(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).P adRight(15,':') .Substring(0,15 );
string b = new string('b',i).P adRight(15,':') .Substring(0,15 );
string c = new string('c',i).P adRight(30,':') .Substring(0,30 );
string str = string.Format(" {0,-10}.....{1,-15}............ ...{2,-30}",a,b,c);
Console.WriteLi ne("{0}",str);
}

Bill
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6488
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me where I can look up explaination on the String.Format, the format string part like "("{0:.#0}". What's the trailing 0 and what is that "0:" means? ...
38
807
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a FormatString, how can you convert the String back to a DateTime? DateTime.Parse(...) doesn't use the FormatString. Now admitedly, if the format string is...
4
2846
by: Chris Dunaway | last post by:
I have a table in the database with a phone number field. The phone number is stored without any punctuation (e. g. 9995551234). I wish to take that string and format it for display (e. g. (999) 555-1234). I know that I can use the .substring method of the string class to get the characters I want and format it: Dim s As String With...
3
1407
by: scorpion53061 | last post by:
I have little hope of resolving this as I have had to contact outside help. But I thought I would post it here to see if anyone could add an idea or solution. 1. I have a form in a Class Control library along with 6 other namespaces. My root functions are in this form and all the other namespaces revolve around this form. I add the...
5
2313
by: AMP | last post by:
Hello, I want to add some variables to a string and this isnt working. textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo"; What am I doing wrong? Thanks Mike
8
4984
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to string and print it. but i want to use the String.Format() method if possible to do it.
3
1876
by: shawrie | last post by:
Hello everyone can anyone please help me? I basically want to set the length of a string variable to help spacing my simple report out. i tried dim test as string(14) but it didnt like that
8
2216
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think that it is because the "mystr" it is already a string. Anyone can help me? Sorry my english....
3
3254
by: 6afraidbecause789 | last post by:
If able, can someone please help make a Where clause that strings together IDs in a multi-select listbox AND includes a date range. I wasn’t thinking when I used the code below that strings together the IDs of Clients from a multi-select listbox in an unbound text field, txtCriteria, on a form that is used to pick different reports. It...
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.