Strign.Format - 参考在哪里? [英] Strign.Format -- where is the reference?

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

问题描述

我想使用String.Format构建一个URL

String.Format(" http://server.com/page.aspx?id = {0} {1: \& optionalParameter = 0;&}",

id,optionalParam);

想法是我要排除& optionalparamter = value部分当optionalParam为负数时,

字符串。

我已经尝试在分号空白后放置任何内容但是输出

(奇怪? )url字符串的减号。

MSDN中的帮助指向VB6格式函数的描述,而

没有给我任何帮助(可以微软pelase更新吗? )。


任何帮助表示赞赏!

谢谢,

mortb


解决方案

我认为没有直接的方法可以做到这一点。

你可以这样做:


string.Format(" http://server.com/page.aspx?id = {0} {1}"

,id

,0x0< = opt ionalParameter

? string.Concat("& optionalParameter =",optionalParameter.ToString())

:string.Empty);


-

问候,

DennisJDMyrén

Oslo Kodebureau

" mortb" < mortb1< noospam< @ hotmail.com>在消息中写道

新闻:OS ************** @ TK2MSFTNGP11.phx.gbl ...

我想要使用String.Format构建URL
String.Format(" http://server.com/page.aspx?id = {0} {1:\& optionalParameter = 0;&}" ;,
id,optionalParam);
我的想法是当OptionalParam为负数时,我想排除
字符串的& optionalparamter = value部分。
我有尝试在分号空格后放置任何内容,但输出
(奇怪?)减号到url字符串。
MSDN中的帮助指向VB6格式函数的描述并且
给出我没有帮助(可以微软pelase更新吗?)。

任何帮助表示赞赏!
谢谢,
mortb



<" mortb" < mortb1< noospam< @ hotmail.com>>写道:

我想使用String.Format构建一个URL
String.Format(" http://server.com/page.aspx?id = {0} {1:\& optionalParameter = 0;&}",
id,optionalParam);
我的想法是要排除& optionalparamter = value部分
optionalParam为负时的字符串。
我已经尝试在分号空白后放置任何内容但是输出
(奇怪?)减号到url字符串。
MSDN中的帮助指向VB6格式函数的描述并且
没有给我帮助(可以微软pelase更新吗?)。




我必须说,这是相当的一种奇怪的格式化方式。

但是,你可以让它工作。试试这个格式字符串:


{1:''& optionalParameter =''0;''''}


- -

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要给我发邮件


DennisMyrén< de **** @ oslokb.no>写道:

我认为没有直接的方法可以做到这一点。
你可以这样做:

string.Format(" http://server.com/page.aspx?id={0}{1}"
,id
,0x0< = optionalParameter
?string.Concat(&& ; optionalParameter =",optionalParameter.ToString())
:string.Empty);




请注意,更具可读性(且完全等效)的方式写

最后一个参数是:


optionalParameter> = 0? "& optionalParameter =" + optionalParameter:""


C / C ++ - 就像避免将变量放在左边的习惯一样

比较的一面是为了避免意外分配在C#中不需要
(除了布尔,我发现它很少见到/ b $ b见'x == true或x == false而不仅仅是x或!x。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www。 pobox.com/~skeet

如果回复小组,请不要给我发邮件


I''d like to build a URL using String.Format
String.Format("http://server.com/page.aspx?id={0}{1:\&optionalParameter=0;&}",
id, optionalParam);
The idea is that I want to exclude the &optionalparamter=value part of the
string when optionalParam is negative.
I''ve tried to place nothing after the semicolon blank but that outputs
(strange?) the minus sign to the url string.
The help in MSDN points to a description of VB6 format function and that
gives me no help (could microsoft pelase update this?).

Any help appreciated!
thanks,
mortb


解决方案

I do not think there is a direct way to do that.
You could, however, do something like:

string.Format("http://server.com/page.aspx?id={0}{1}"
, id
, 0x0 <= optionalParameter
? string.Concat("&optionalParameter=", optionalParameter.ToString())
: string.Empty);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"mortb" <mortb1<noospam<@hotmail.com> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl...

I''d like to build a URL using String.Format
String.Format("http://server.com/page.aspx?id={0}{1:\&optionalParameter=0;&}",
id, optionalParam);
The idea is that I want to exclude the &optionalparamter=value part of the
string when optionalParam is negative.
I''ve tried to place nothing after the semicolon blank but that outputs
(strange?) the minus sign to the url string.
The help in MSDN points to a description of VB6 format function and that
gives me no help (could microsoft pelase update this?).

Any help appreciated!
thanks,
mortb



<"mortb" <mortb1<noospam<@hotmail.com>> wrote:

I''d like to build a URL using String.Format
String.Format("http://server.com/page.aspx?id={0}{1:\&optionalParameter=0;&}",
id, optionalParam);
The idea is that I want to exclude the &optionalparamter=value part of the
string when optionalParam is negative.
I''ve tried to place nothing after the semicolon blank but that outputs
(strange?) the minus sign to the url string.
The help in MSDN points to a description of VB6 format function and that
gives me no help (could microsoft pelase update this?).



I must say, this is quite an odd way of going about formatting.
However, you can get it to work. Try this for the format string:

{1:''&optionalParameter=''0;''''}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Dennis Myrén <de****@oslokb.no> wrote:

I do not think there is a direct way to do that.
You could, however, do something like:

string.Format("http://server.com/page.aspx?id={0}{1}"
, id
, 0x0 <= optionalParameter
? string.Concat("&optionalParameter=", optionalParameter.ToString())
: string.Empty);



Note that a more readable (and entirely equivalent) way of writing the
last parameter would be:

optionalParameter >= 0 ? "&optionalParameter="+optionalParameter : ""

The C/C++-like habit of avoiding putting variables on the left hand
side of comparisons in order to avoid accidental assignment is
unnecessary in C# (aside from for booleans, where I find it''s rare to
see "x==true" or "x==false" rather than just "x" or "!x").

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于Strign.Format - 参考在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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