字符串格式帮助 [英] String Format help

查看:90
本文介绍了字符串格式帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串格式公式写一个文本文件

下面对我不起作用

pos 1到10名

pos 15到30姓氏

pos 45到75岁工作

亚当史密斯程序员


string fmt =" {1,10} {15- 30} {45,75}" //没有任何帮助吗?

sb.Append(string.Format(fmt,Adam",smith,progra mmer));

解决方案

嗨Matt,


首先,你使用String.Format函数都错了。请看下面的



http://msdn.microsoft.com/library/de...ormattopic.asp


你只能将一个字符串传递给函数。


假设你没有使用文字字符串,你应该能够

使用String.PadRight()方法构建字符串。示例:


string firstName =" Adam";

string lastName =" Smith" ;;

string job =" ;程序员;


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

job.PadRight(75);


-

HTH,


Kevin Spencer

Microsoft MVP

..Net开发商

观看时钟永不沸腾。


" Matt" <我******* @ Hotmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

字符串格式公式写一个文本文件
下面对我不起作用
pos 1到10名
pos 15到30 lastname
pos 45到75 job

Adam smith程序员

string fmt =" {1,10} {15-30} {45,75}" //没有任何帮助吗?

sb.Append(string.Format(fmt," Adam"," smith"," progra mmer"));



这对我有用:

Console.WriteLine(" 123456789a123456789b123456789c1 23456789d123456789e1234567

89f123456789g123456");

Console.WriteLine(" {0,-10} .... {1,-15} ............... {2,-30} |" ,Adam,

" Smith",Programmer);

在每个{n,m}中,第一个数字是参数的位置,第二个

数字是字段的长度,负数意义填充在右边

(正数会填充在左边)。

-

真相,

James Curran

[以前的VC ++ MVP]


主页: www.noveltheory.com 工作: www.njtheater.com

博客: www。 honestillusion.com 日工作: www.partsearch.com

马特 <我******* @ Hotmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

字符串格式公式写一个文本文件
下面对我不起作用
pos 1到10名
pos 15到30 lastname
pos 45到75 job

Adam smith程序员

string fmt =" {1,10} {15-30} {45,75}" //没有任何帮助吗?

sb.Append(string.Format(fmt," Adam"," smith"," progra mmer"));



" Kevin Spencer" <柯*** @ DIESPAMMERSDIEtakempis.com>在消息中写道

新闻:#D ************* @ TK2MSFTNGP10.phx.gbl ...

你只能将一个字符串传递给函数。




废话。您可以将任意数量的参数传递给String.Format,

,其中任何一个都可以是字符串。


-

真相,

James Curran

[以前的VC ++ MVP]


主页: www.noveltheory.com 工作: www.njtheater.com

博客: www .honestillusion.com 日工作: www.partsearch.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(string.Format(fmt,"Adam","smith","progra mmer"));

解决方案

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.PadRight(10) + lastName.PadRight(30) +
job.PadRight(75);

--
HTH,

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

"Matt" <me*******@Hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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(string.Format(fmt,"Adam","smith","progra mmer"));



This worked for me:
Console.WriteLine("123456789a123456789b123456789c1 23456789d123456789e1234567
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
"Matt" <me*******@Hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.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(string.Format(fmt,"Adam","smith","progra mmer"));



"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:#D*************@TK2MSFTNGP10.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


这篇关于字符串格式帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆