的String.Format() [英] String.Format()

查看:89
本文介绍了的String.Format()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[你怎么能这样做]:


string.Format(" MyString {0}",var);


[但你不能这样做]:


String str =" MyString {0}";

str.Format(var );


换句话说,为什么格式化字符串类的方法,但它不能在字符串类的实例上使用
?< br>

[How come you can do this]:

string.Format("MyString {0}", var);

[But you can''t do this]:

String str = "MyString {0}";
str.Format(var);

In otherwords why is Format a method of the string class but it can''t
be used on an instance of the string class?

推荐答案

在其他方面,为什么格式化一个字符串类的方法,但它不能成为用于字符串类的实例?
In otherwords why is Format a method of the string class but it can''t
be used on an instance of the string class?




因为它是静态的。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



Because it''s static.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


试试这个:


Int32 Num = 99;

String str = " ;;


str = str.Format(" Hello:{0}",Num);


str = ==> 你好:99


尽管方法是静态的,你可以使用它的实例访问该方法,但是,方法是静态的,实例不是必需的,


祝你好运!


" Phill" < WA ******** @ yahoo.com>在消息中写道

news:ac ************************** @ posting.google.c om ...
Try this:

Int32 Num = 99;
String str = "";

str = str.Format("Hello: {0}", Num);

str ===> "Hello: 99"

Eventhough a method is static you can access that method with its instance,
though, method is static an instance is NOT REQUIRED,

Good Luck !

"Phill" <wa********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
[你怎么能这样做]:

string.Format(" MyString {0}",var);

[但你不能这样做:

String str =" MyString {0}";
str.Format(var);

其他为什么格式化字符串类的方法,但它不能用于字符串类的实例?
[How come you can do this]:

string.Format("MyString {0}", var);

[But you can''t do this]:

String str = "MyString {0}";
str.Format(var);

In otherwords why is Format a method of the string class but it can''t
be used on an instance of the string class?



I认为问题是

为什么格式是静态的?


为什么你不能这样做:


string fmt ="<%d>" ;;

string result = fmt.Format(2); // =>结果="< 2>" ;;


为什么要写:


string result = string.Format(fmt ,2);


为什么''替换()''(用字符串替换子字符串)

字符串实例的方法,

但''Format()''(用值替换格式占位符)静态方法?


Nadav
I think the question is
WHY is Format static?

Why can''t you do:

string fmt="<%d>";
string result=fmt.Format(2); // => result="<2>";

Why do you have to write:

string result=string.Format(fmt,2);

Why is ''Replace()'' (replace a substring with a string) a method of the
string instance,
but ''Format()'' (replace format place holders with values) a static method?

Nadav

这篇关于的String.Format()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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