简体   繁体   English

C# string.Join 与多个分隔符

[英]C# string.Join with muliple delimiter

I searched through Google and all the other links, but couldn't find out if this is possible.我通过 Google 和所有其他链接进行了搜索,但不知道这是否可行。

string.Join method linkhttp://msdn.microsoft.com/en-us/library/system.string.join.aspx string.Join方法链接http://msdn.microsoft.com/en-us/library/system.string.join.aspx

Join(String, String[]) , I understand that the first argument ( String ) is the delimiter and the second argument ( String[] ) is the array which we want to join. Join(String, String[]) ,我知道第一个参数( String )是分隔符,第二个参数( String[] )是我们想要加入的数组。 Is it possible to join it with 2 different string/delimiter?是否可以使用 2 个不同的字符串/分隔符加入它? for eg my array>例如我的阵列>

array[0] = A;
array[1] = Alpha; 
array[2] = B; 
array[3] = Bravo;

such than the end product is:最终产品是:

A = Alpha, B= Bravo, 

So it will be 2 strings, "=" and ",".所以它将是 2 个字符串,“=”和“,”。 Any suggestion to do this?有什么建议可以这样做吗?

UPADTED: Both Codes if you don't want , at end.更新:如果你不想要,最后两个代码

You can do like this, But suggested to pair key and value or use 2-dimensional array.你可以这样做,但建议配对keyvalue或使用二维数组。

CODE:代码:

int i=0;
string str = "";
var arr = new string[] { "A", "Alpha", "B", "Bravo" };

foreach (var item in arr)
 {
    str += item;
    if (i < arr.Length-1)
    {
       if (i % 2 == 0)                
         str += " = ";               
       else
         str += ", ";          
    }
    i++;
 }
//output: A = Alpha, B = Bravo


By this way you can also use for multiple delimiters.通过这种方式,您还可以使用多个分隔符。

Example:例子:

int i=0;
string str = "";
var arr = new string[] { "A", "Alpha", "1", "B", "Bravo", "2" };

var delimiters = new string[] { " = ", ", ", ":" };

foreach (var item in arr)
 {
    str += item;
    if (i < arr.Length-1)
    {
        if (i % 3 == 0)
           str += delimiters[0];
        else if (i % 3 == 1)
           str += delimiters[1];                        
        else
           str += delimiters[2];                        
     }
     i++;
 }
//output: A = Alpha:1, B = Bravo:2

You need to group your values pair-wise, use string.Join on each pair (or just use key + " = " + value ) and then use string.Join on the results.您需要成对地对您的值进行分组,在每一对上使用string.Join (或仅使用key + " = " + value ),然后在结果上使用string.Join

String.Join doesn't support this functionality out of the box. String.Join不支持开箱即用的此功能。

Alternatively, you could just write your own loop to do this.或者,您可以编写自己的循环来执行此操作。

I don't think that's possible.我不认为那是可能的。 You could just Join on the , and then loop through and replace every other , with a = .你可以只Join,然后通过循环和更换每隔,=

This extensions method should do the trick:这个扩展方法应该可以解决问题:

public static class Extensions
{
    public static string Join(this object[] array, string[] delimiters)
    {
        var returnString = "";
        var s = new Queue<string>(delimiters);
        for (var i = 0; i < array.Count(); i++)
        {
            var delim = s.Dequeue();
            returnString += array[i] + delim;
            s.Enqueue(delim);
        }
        while (s.Count > 0) returnString = returnString.TrimEnd(s.Dequeue().ToCharArray());
        return returnString;

    }
}

You can then use array.Join(new string[] {"=", ","}) The extension-method alternates between the delimiters.然后可以使用array.Join(new string[] {"=", ","})扩展方法在分隔符之间交替。

Hope that helps!希望有帮助!

 StringBuilder output = new StringBuilder();

 for(int i = 0; i <= array.length; i++)
 {
         output.Append(array[i] + "=" + array[i+1]);
         if(i != array.length) output.Append(",");
 }

 Response.Write(output.ToString());

Hope this is what you're looking for ;)希望这就是你要找的 ;)

string[] str = { "A", "Alpha", "B", "Beta" };
string[] dl = { " = ", ", " };
string result = "";
for (int i = 0; i < str.Length; i++)
{
    result += str[i] + dl[0] + str[i + 1];
    if(i != str.Length - 2){
        result += dl[1];
    }
    i++;
}
return result;

^^ ^^

@javed with for the code is a bit simpler @javed with 代码稍微简单一点

            for (int i = 0; i < query.Length; i++)
            {
                endpoint += query[i];

                if (i < query.Length - 1)
                {
                    if (i % 2 == 0)
                        endpoint += "=";
                    else
                        endpoint += "&";
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM