Strign.Format -- where is the reference?

  • Thread starter Thread starter mortb
  • Start date Start date
M

mortb

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);
 
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;''}
 
Dennis Myrén said:
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").
 
I could not agree more!
/mortb

Dennis Myrén said:
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").
 
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;''}

Thank you!

cheers,
mortb
 
Jon said:
Note that a more readable (and entirely equivalent) way of writing the
last parameter would be:
optionalParameter >= 0 ? "&optionalParameter="+optionalParameter : ""

Thank you for pointing that out for me.

I am very aware of that fact though.
This is coming down to a matter of favour of coding styles.
I personally like to place constants on the left
even if i could place them on the right side.

I think System.String.Concat is prettier
than "manual" string concatenation using operator +.
That is why i always use that instead.
The only exception is when the strings concatenated is constant strings,
because then the compiler is free to optimize the statement by actually
concatenating them during compilation, and I do not think that is the case
with String.Concat.

This is the second time you are noticing me of the constants on the left
thing.
I interpret that as you do not want me to use that syntax in code posts.
I will try to avoid it from now on.


By the way, the {1:'&optionalParameter='0;''} solution was nice!





--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
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").
 
Dennis Myrén said:
Thank you for pointing that out for me.

I am very aware of that fact though.
Goodo.

This is coming down to a matter of favour of coding styles.
I personally like to place constants on the left
even if i could place them on the right side.

Fair enough, although I think most people find it less readable that
way.
I think System.String.Concat is prettier
than "manual" string concatenation using operator +.

Again, I think most people would disagree with you in terms of
readability. That's entirely your prerogative though - I'm certainly
not going to try to tell you what *you* find more readable :)
That is why i always use that instead.
The only exception is when the strings concatenated is constant strings,
because then the compiler is free to optimize the statement by actually
concatenating them during compilation, and I do not think that is the case
with String.Concat.
Indeed.

This is the second time you are noticing me of the constants on the left
thing.
I interpret that as you do not want me to use that syntax in code posts.
I will try to avoid it from now on.

Feel free to keep doing it - I wasn't sure whether you were just doing
it from C/C++ habits unnecessarily though. (I don't keep track of who
I've explained it to :) I'm certainly in no position to demand people
format their code in a particular way (although if they're asking me a
question it helps if they make it readable).
By the way, the {1:'&optionalParameter='0;''} solution was nice!

I'm not sure I'd actually use the word "nice" here - it's a solution
which is relatively hard to read, IMO, but it seems to be the way the
OP wanted to play things.
 
But of course :)

But as I am not a big fan of writing hex values in my source I think parm <
0 is more readable.
It tells me more about what the intention was when I wrote the code and thus
it decreases the time to "re-understand" the code some seconds when I open
the file to fix some bugs.

cheers,
mortb
 
Odd?
The variable is initialized to -1 and then I don't want it to be passed
along with the URL

thanks again,
mortb
 
I will attempt to post more readable code snippets from now on.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Dennis Myrén said:
Thank you for pointing that out for me.

I am very aware of that fact though.
Goodo.

This is coming down to a matter of favour of coding styles.
I personally like to place constants on the left
even if i could place them on the right side.

Fair enough, although I think most people find it less readable that
way.
I think System.String.Concat is prettier
than "manual" string concatenation using operator +.

Again, I think most people would disagree with you in terms of
readability. That's entirely your prerogative though - I'm certainly
not going to try to tell you what *you* find more readable :)
That is why i always use that instead.
The only exception is when the strings concatenated is constant strings,
because then the compiler is free to optimize the statement by actually
concatenating them during compilation, and I do not think that is the case
with String.Concat.
Indeed.

This is the second time you are noticing me of the constants on the left
thing.
I interpret that as you do not want me to use that syntax in code posts.
I will try to avoid it from now on.

Feel free to keep doing it - I wasn't sure whether you were just doing
it from C/C++ habits unnecessarily though. (I don't keep track of who
I've explained it to :) I'm certainly in no position to demand people
format their code in a particular way (although if they're asking me a
question it helps if they make it readable).
By the way, the {1:'&optionalParameter='0;''} solution was nice!

I'm not sure I'd actually use the word "nice" here - it's a solution
which is relatively hard to read, IMO, but it seems to be the way the
OP wanted to play things.
 
Odd?
The variable is initialized to -1 and then I don't want it to be passed
along with the URL

But it's an odd way of expressing whether or not you want something to
be included. It's not exactly what custom number formats are usually
used for. It's kinda neat, but I think it's less obvious what's going
on (unless you're using custom number format strings all the time) than
making it clear by making it a string parameter of value
optionalParameter=... or "".
 
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").

Note that there is a school of thought that says that you should write code
like (replacing the left side with the right side below):

(foo > 0) --> (0 < foo)
(foo > 0 && foo < 10) --> (0 < foo && foo < 10)

The idea is to favour "less than" so that the test can be thought of as
using a number scale going from left to right; e.g.:

0-1-2-3-4-5-6-7-8-9-10
+------foo------+
 
Back
Top