0

我有下一个字符串:

public const string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/";
public const string LoginURL = "{0}sessions";

我想创建一个 LoginURL 的 URL,其中 {0} 是 BaseAddress。逻辑方法不起作用:

string str = string.Format(BaseAddress, LoginURL);

它只打印 BaseAddress。如果我这样做,它将起作用:

string str = string.Format(LoginURL, BaseAddress); //Is this the correct way?
4

5 回答 5

4

It's not logical way, you are defining parameter in LoginUrl not in BaseAddress, so it's working correctly

If you want it work logicaly do it that way:

public const string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/{0}";
public const string LoginURL = "sessions";

string.Format is replacing {0} with first parameter, so it will now work like you want it to work.

于 2013-09-12T13:02:56.280 回答
2
string str = string.Format(LoginURL, BaseAddress); //Is this the correct way?

Yes, that is the correct way. Although it doesn't follow the usual pattern people use. Please read the documentation for the function located here: http://msdn.microsoft.com/en-us/library/system.string.format.aspx

The first argument is the Format, the second are the arguments/replacement values. Assuming you want to maintain a constant format, that is usually a hardcoded in the argument as such:

string str = string.Format("{0}sessions", BaseAddress);

Being as it seems the base address is normally more consistent (but may still be variable) an approach such as the following may be better:

public const string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/";
public const string LoginURL = "sessions";
string str = string.Format("{0}{1}", BaseAddress, LoginURL);

If your end-goal is just URL combination rather than an exercise using string.Format, the following may still be a better approach:

Uri baseUri = new Uri("http://test.i-swarm.com/i-swarm/api/v1/");
Uri myUri = new Uri(baseUri, "sessions");

That way you don't have to worry about whether or not a separator was used in the base address/relative address/etc.

于 2013-09-12T13:03:15.753 回答
2

没有合乎逻辑的方法,只有一种方法可以遵守String.Format工作原理。String.Format定义为使第一个参数包含要插入参数的字符串,然后是参数列表。

如果你像你一样定义字符串,唯一正确的方法是

string str = string.Format(LoginURL, BaseAddress);

BaseAddress但是,如果是格式字符串,它会更直观:

public const string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/{0}";
public const string LoginURL = "sessions";

所以你可以写

string str = String.Format(BaseAddress, LoginURL);
于 2013-09-12T13:04:12.400 回答
1

Yes the correct way is:

string str = String.Format(LoginURL, BaseAddress);

The format string should always be the first parameter.

Though I feel that:

string str = String.Format("{0}sessions", BaseAddress);

Is more readable.

于 2013-09-12T13:03:52.127 回答
0

您可以使用 C# 的插值字符串。

string BaseAddress = "http://test.i-swarm.com/i-swarm/api/v1/";
string LoginURL = $"{BaseAddress}sessions";

你可以有多个参数。这是一种更安全的格式化字符串的方法,因为在使用 string.Format 时,您需要确保参数的顺序正确。使用这种新方式,您不必这样做。

string foo = "foo";
int bar = 4;
string result = $"{bar} hello {foo}";

参考:

于 2022-01-16T02:14:55.437 回答