簡體   English   中英

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,我想用一個空字符串替換括號內的每個數字。 我的返回值應該是:

"state=&country=&geolocation=&city="

如果其大小為1,並且列表中的第一個成員為“ 21,27”,則我的返回字符串應為:

"state=&country=&geolocation=21,27&city="

依此類推...為此,我可以使用循環或正則表達式,但是我一直在想是否有更好的方法,也許是Linq解決方案? 我所知道的是postData最多可以有多少個數字,在我的情況下是4。 同樣,我可以使用循環或正則表達式來做到這一點,但是我試圖使其盡可能短

編輯:postData字符串可能會有所不同。 那只是一個例子。 它的大小或內容可能不同

我所知道的是postData最多可以有多少個數字

然后呢:

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);

如果允許您使用C#6編寫,那么我建議您使用以下快速解決方案

定義一個類,該類具有您所引用的參數作為屬性,並以一種返回所需URL的方式對ToString方法進行處理。

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#編寫,則必須對類的定義進行一些修改,如下所示:

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);      
    }
}

但是, 最好的解決方案可以在Henri格式程序Named Formats Redux中找到。 如果實現此功能,則可以將其稱為擴展方法,如下所示:

var template = "state={state}&country={country}&geolocation={geo}&city={city}";
var customUrl = template.HenriFormat(new { geo = "21,72"});

我之所以說這是最好的解決方案,是因為您只需實現一次即可,並且可以在任何地方使用它,而不必為上述情況實現自定義類。

您可以使用如下形式:

private string UnknownSizeStringFormat(string postData, params string[] stringsToReplace)
{
    string[] temp = { "", "", "", "" };
    Array.ConstrainedCopy(stringsToReplace, 0, temp, 0, stringsToReplace.Length);
    return string.format(postData, temp);
}

我將從基於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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM