C# 字符串格式未知大小
C# string format unknown size
假设我有以下字符串:
string postData = "state={2}&country={3}&geolocation={0}&city={1}";
我还有另一个字符串列表。在这种情况下,它的大小最多可以是 4。
我正在尝试创建一种方法来替换我的 postData 变量中的数字,具体取决于列表大小。类似于以下方法:
private string UnknownSizeStringFormat(string postData, params string[] stringsToReplace)
{
return string.format(postData, stringsToReplace);
}
只要列表的大小为 4,上述方法就有效 。问题是,在第一次调用时,我的列表大小可能小于 4,所以如果它是 0,例如,我想用空字符串替换括号内的每个数字。我的 return 值应该是:
"state=&country=&geolocation=&city="
如果它的大小是一个,并且列表中的第一个成员是“21,27”,我的 return 字符串应该是:
"state=&country=&geolocation=21,27&city="
等等...
我可以为此目的使用循环或正则表达式,但我一直想知道是否有更好的方法,也许是 Linq 解决方案?我所知道的是 postData 最多可以有多少个数字,在我的例子中是 4。
同样,我可以用循环或正则表达式来做到这一点,但我试图让它尽可能短
编辑:postData 字符串可能会有所不同。那只是一个例子。它的大小或内容可能不同
如果你被允许用 C#6 编写,那么我建议你使用以下快速解决方案
定义一个 class,它具有您引用的参数的属性,并以这样的方式验证 ToString
方法以 return 您想要的 url。
public class CustomUrl
{
public string State {get;set;}
public string Country { get;set;}
public string Geolocation {get;set;}
public string City {get;set;}
public ovveride string ToString() =>
$"state={State}&country={Country}&geolocation={Geolocation}&city={City}";
}
您可以将其用作:
var customUrl = new CustomUrl
{
Geolocation = "21,27";
}
然后调用customUrl.ToString()
,你会得到:
"state=&country=&geolocation=21,27&city="
正在创建另一个客户 url 作为:
var customUrl = new CustomUrl();
调用 customUrl.ToString()
你会得到:
"state=&country=&geolocation=&city="
如果你不允许用C#写,你必须修改一下class的定义,如下所示:
public class CustomUrl
{
public string State {get;set;}
public string Country { get;set;}
public string Geolocation {get;set;}
public string City {get;set;}
public ovveride string ToString()
{
retrun string.Format("state={0}&country={1}&geolocation={2}&city={3}",State,Country,Geolocation,City);
}
}
但是,最佳解决方案 可以在 Named Formats Redux,Henri 格式化程序中找到。如果你实现它,你可以将它作为扩展方法调用,如下所示:
var template = "state={state}&country={country}&geolocation={geo}&city={city}";
var customUrl = template.HenriFormat(new { geo = "21,72"});
我之所以说这是最好的解决方案,是因为您只需实施一次就可以在任何地方使用它,而不必针对上述情况实施自定义 class。
我会根据 stringsToReplace.Length 调整您的 postData。您可以使用 switch/case 方法来控制它。此为未经测试的代码,请结合debug使用进行验证。
string postData = "state={2}&country={3}&geolocation={0}&city={1}";
private string UnknownSizeStringFormat(string postData, params string[] stringsToReplace)
{
switch(stringsToReplace.Length){
case 0:
postData = "state={0}&country={0}&geolocation={0}&city={0}";
break;
case 1:
postData = "state={0}&country={0}&geolocation={1}&city={0}";
break;
case 2:
postData = "state={2}&country={0}&geolocation={1}&city={0}";
break;
case 3:
postData = "state={2}&country={3}&geolocation={1}&city={0}";
break;
case 4:
postData = "state={2}&country={3}&geolocation={1}&city={4}";
break;
return string.format(postData, String.Empty, stringsToReplace);
}
}
你可以使用这样的东西:
private string UnknownSizeStringFormat(string postData, params string[] stringsToReplace)
{
string[] temp = { "", "", "", "" };
Array.ConstrainedCopy(stringsToReplace, 0, temp, 0, stringsToReplace.Length);
return string.format(postData, temp);
}
What I do know is how many numbers postData could have at most
那这个怎么样:
static string UnknownSizeStringFormat(string format, int maxArgs, params object[] args)
{
Array.Resize(ref args, maxArgs);
return string.Format(format, args);
}
所以你可以使用:
string postData = "state={2}&country={3}&geolocation={0}&city={1}";
var result = UnknownSizeStringFormat(postData, 4);
假设我有以下字符串:
string postData = "state={2}&country={3}&geolocation={0}&city={1}";
我还有另一个字符串列表。在这种情况下,它的大小最多可以是 4。 我正在尝试创建一种方法来替换我的 postData 变量中的数字,具体取决于列表大小。类似于以下方法:
private string UnknownSizeStringFormat(string postData, params string[] stringsToReplace)
{
return string.format(postData, stringsToReplace);
}
只要列表的大小为 4,上述方法就有效 。问题是,在第一次调用时,我的列表大小可能小于 4,所以如果它是 0,例如,我想用空字符串替换括号内的每个数字。我的 return 值应该是:
"state=&country=&geolocation=&city="
如果它的大小是一个,并且列表中的第一个成员是“21,27”,我的 return 字符串应该是:
"state=&country=&geolocation=21,27&city="
等等... 我可以为此目的使用循环或正则表达式,但我一直想知道是否有更好的方法,也许是 Linq 解决方案?我所知道的是 postData 最多可以有多少个数字,在我的例子中是 4。 同样,我可以用循环或正则表达式来做到这一点,但我试图让它尽可能短
编辑:postData 字符串可能会有所不同。那只是一个例子。它的大小或内容可能不同
如果你被允许用 C#6 编写,那么我建议你使用以下快速解决方案
定义一个 class,它具有您引用的参数的属性,并以这样的方式验证 ToString
方法以 return 您想要的 url。
public class CustomUrl
{
public string State {get;set;}
public string Country { get;set;}
public string Geolocation {get;set;}
public string City {get;set;}
public ovveride string ToString() =>
$"state={State}&country={Country}&geolocation={Geolocation}&city={City}";
}
您可以将其用作:
var customUrl = new CustomUrl
{
Geolocation = "21,27";
}
然后调用customUrl.ToString()
,你会得到:
"state=&country=&geolocation=21,27&city="
正在创建另一个客户 url 作为:
var customUrl = new CustomUrl();
调用 customUrl.ToString()
你会得到:
"state=&country=&geolocation=&city="
如果你不允许用C#写,你必须修改一下class的定义,如下所示:
public class CustomUrl
{
public string State {get;set;}
public string Country { get;set;}
public string Geolocation {get;set;}
public string City {get;set;}
public ovveride string ToString()
{
retrun string.Format("state={0}&country={1}&geolocation={2}&city={3}",State,Country,Geolocation,City);
}
}
但是,最佳解决方案 可以在 Named Formats Redux,Henri 格式化程序中找到。如果你实现它,你可以将它作为扩展方法调用,如下所示:
var template = "state={state}&country={country}&geolocation={geo}&city={city}";
var customUrl = template.HenriFormat(new { geo = "21,72"});
我之所以说这是最好的解决方案,是因为您只需实施一次就可以在任何地方使用它,而不必针对上述情况实施自定义 class。
我会根据 stringsToReplace.Length 调整您的 postData。您可以使用 switch/case 方法来控制它。此为未经测试的代码,请结合debug使用进行验证。
string postData = "state={2}&country={3}&geolocation={0}&city={1}";
private string UnknownSizeStringFormat(string postData, params string[] stringsToReplace)
{
switch(stringsToReplace.Length){
case 0:
postData = "state={0}&country={0}&geolocation={0}&city={0}";
break;
case 1:
postData = "state={0}&country={0}&geolocation={1}&city={0}";
break;
case 2:
postData = "state={2}&country={0}&geolocation={1}&city={0}";
break;
case 3:
postData = "state={2}&country={3}&geolocation={1}&city={0}";
break;
case 4:
postData = "state={2}&country={3}&geolocation={1}&city={4}";
break;
return string.format(postData, String.Empty, stringsToReplace);
}
}
你可以使用这样的东西:
private string UnknownSizeStringFormat(string postData, params string[] stringsToReplace)
{
string[] temp = { "", "", "", "" };
Array.ConstrainedCopy(stringsToReplace, 0, temp, 0, stringsToReplace.Length);
return string.format(postData, temp);
}
What I do know is how many numbers postData could have at most
那这个怎么样:
static string UnknownSizeStringFormat(string format, int maxArgs, params object[] args)
{
Array.Resize(ref args, maxArgs);
return string.Format(format, args);
}
所以你可以使用:
string postData = "state={2}&country={3}&geolocation={0}&city={1}";
var result = UnknownSizeStringFormat(postData, 4);