How to join int[] to a character separated string in .NET?

后端 未结 11 819
广开言路
广开言路 2020-11-29 19:17

I have an array of integers:

int[] number = new int[] { 2,3,6,7 };

What is the easiest way of converting these into a single string where

11条回答
  •  独厮守ぢ
    2020-11-29 20:05

    In .NET 4.0, string join has an overload for params object[], so it's as simple as:

    int[] ids = new int[] { 1, 2, 3 };
    string.Join(",", ids);
    

    example

    int[] ids = new int[] { 1, 2, 3 };
    System.Data.Common.DbCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT * FROM some_table WHERE id_column IN (@bla)");
    cmd.CommandText = cmd.CommandText.Replace("@bla",  string.Join(",", ids));
    

    In .NET 2.0, it's a tiny little bit more difficult, since there's no such overload. So you need your own generic method:

    public static string JoinArray(string separator, T[] inputTypeArray)
    {
        string strRetValue = null;
        System.Collections.Generic.List ls = new System.Collections.Generic.List();
    
        for (int i = 0; i < inputTypeArray.Length; ++i)
        {
            string str = System.Convert.ToString(inputTypeArray[i], System.Globalization.CultureInfo.InvariantCulture);
    
            if (!string.IsNullOrEmpty(str))
            { 
                // SQL-Escape
                // if (typeof(T) == typeof(string))
                //    str = str.Replace("'", "''");
    
                ls.Add(str);
            } // End if (!string.IsNullOrEmpty(str))
    
        } // Next i 
    
        strRetValue= string.Join(separator, ls.ToArray());
        ls.Clear();
        ls = null;
    
        return strRetValue;
    }
    

    In .NET 3.5, you can use extension methods:

    public static class ArrayEx
    {
    
        public static string JoinArray(this T[] inputTypeArray, string separator)
        {
            string strRetValue = null;
            System.Collections.Generic.List ls = new System.Collections.Generic.List();
    
            for (int i = 0; i < inputTypeArray.Length; ++i)
            {
                string str = System.Convert.ToString(inputTypeArray[i], System.Globalization.CultureInfo.InvariantCulture);
    
                if (!string.IsNullOrEmpty(str))
                { 
                    // SQL-Escape
                    // if (typeof(T) == typeof(string))
                    //    str = str.Replace("'", "''");
    
                    ls.Add(str);
                } // End if (!string.IsNullOrEmpty(str))
    
            } // Next i 
    
            strRetValue= string.Join(separator, ls.ToArray());
            ls.Clear();
            ls = null;
    
            return strRetValue;
        }
    
    }
    

    So you can use the JoinArray extension-method.

    int[] ids = new int[] { 1, 2, 3 };
    string strIdList = ids.JoinArray(",");
    

    You can also use that extension method in .NET 2.0, if you add the ExtensionAttribute to your code:

    // you need this once (only), and it must be in this namespace
    namespace System.Runtime.CompilerServices
    {
        [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
        public sealed class ExtensionAttribute : Attribute {}
    }
    

提交回复
热议问题