แปลงรายการเป็นสตริงที่คั่นด้วยเครื่องหมายจุลภาค


158

รหัสของฉันเป็นดังนี้:

public void ReadListItem()
{
     List<uint> lst = new List<uint>() { 1, 2, 3, 4, 5 };
     string str = string.Empty;
     foreach (var item in lst)
         str = str + item + ",";

     str = str.Remove(str.Length - 1);
     Console.WriteLine(str);
}

เอาท์พุท: 1,2,3,4,5

วิธีที่ง่ายที่สุดในการแปลงList<uint>เป็นสตริงคั่นด้วยเครื่องหมายจุลภาคคืออะไร?


8
String.Joinคือทั้งหมดที่คุณต้องการ
asawyer

8
var csvString = String.Join(",", lst);ควรทำมัน
Mithrandir

2
สำหรับทุกคนที่ต้องการเปิดใหม่หากยังไม่แปลเป็นภาษาท้องถิ่นซ้ำซ้อน: stackoverflow.com/questions/799446/…
Tim Schmelter

คำตอบ:


319

สนุก!

Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));

พารามิเตอร์แรก: ","
พารามิเตอร์ที่สอง:new List<uint> { 1, 2, 3, 4, 5 })

String.Joinจะรับรายการเป็นพารามิเตอร์ตัวที่สองและเข้าร่วมองค์ประกอบทั้งหมดโดยใช้สตริงที่ส่งผ่านเป็นพารามิเตอร์แรกในสตริงเดียว


11
ใน. NET 3.5 และต่ำกว่าคุณต้องแปลงรายการของคุณเป็นอาเรย์อย่างชัดเจนlst.ToArray()เนื่องจากยังไม่มีการโอเวอร์โหลดโดยตรง
Anton

69

คุณสามารถใช้วิธีการString.Joinเพื่อรวมรายการ:

var str = String.Join(",", lst);

สิ่งนี้ไม่ได้ผลสำหรับฉัน มันสร้างชื่อของคอลเลกชันมากกว่าวัตถุในรายการ
Rani Radcliff

25

การใช้ String.Join

string.Join<string>(",", lst );

การใช้ Linq Aggregation

lst .Aggregate((a, x) => a + "," + x);

1
ฉันมีรายการประเภท int32 เมื่อฉันใช้ฟังก์ชั่นรวมที่คุณพูดถึงมันจะพูดว่า "ไม่สามารถแปลงนิพจน์แลมบ์ดาให้เป็นประเภทตัวแทน 'System.Func <int, int, int>' เพราะบางประเภทผลตอบแทนในบล็อกไม่ได้แปลงโดยอ้อมกลับประเภทตัวแทน" "ไม่สามารถแปลงประเภท 'string' เป็น 'int'" โดยนัยไม่ได้
Hari

1
@Hari คุณต้องแปลงเป็นค่าสตริงก่อนที่คุณจะ Aggragate เป็นสตริง ดังนั้นคุณสามารถทำสิ่งนี้: list.Select (x => string.Format ("{0}: {1}", x.Key, x.Value)) รวม ((a, x) => a + " , "+ x);
เดิมพัน

11

หากคุณมีคอลเล็กชัน ints:

List<int> customerIds= new List<int>() { 1,2,3,3,4,5,6,7,8,9 };  

คุณสามารถใช้string.Joinเพื่อรับสาย:

var result = String.Join(",", customerIds);

สนุก!


9

ติดตามสิ่งนี้:

       List<string> name = new List<string>();

        name.Add("Latif");
        name.Add("Ram");
        name.Add("Adam");
        string nameOfString = (string.Join(",", name.Select(x => x.ToString()).ToArray()));

4
          @{  var result = string.Join(",", @user.UserRoles.Select(x => x.Role.RoleName));
              @result

           }

ฉันใช้ใน MVC Razor View เพื่อประเมินและพิมพ์บทบาททั้งหมดคั่นด้วยเครื่องหมายจุลภาค



2

คุณสามารถดูตัวอย่างด้านล่างเพื่อรับอาร์เรย์สตริงที่คั่นด้วยเครื่องหมายจุลภาคจากรายการ

ตัวอย่าง:

List<string> testList= new List<string>();
testList.Add("Apple"); // Add string 1
testList.Add("Banana"); // 2
testList.Add("Mango"); // 3
testList.Add("Blue Berry"); // 4
testList.Add("Water Melon"); // 5

string JoinDataString = string.Join(",", testList.ToArray());


1

เราสามารถลองทำเช่นนี้เพื่อแยกรายการเข้าด้วยเครื่องหมายจุลภาค

string stations = 
haul.Routes != null && haul.Routes.Count > 0 ?String.Join(",",haul.Routes.Select(y => 
y.RouteCode).ToList()) : string.Empty;

0

คุณสามารถใช้ google-collection.jar ซึ่งมีคลาสยูทิลิตี้ที่เรียกว่า Joiner

 String commaSepString=Joiner.on(",").join(lst);

หรือ

คุณสามารถใช้คลาส StringUtils ซึ่งมีฟังก์ชั่นที่เรียกว่าเข้าร่วมเพื่อใช้ประโยชน์จากคลาส StringUtils คุณต้องใช้ common-lang3.jar

String commaSepString=StringUtils.join(lst, ',');

สำหรับการอ้างอิงอ้างอิงลิงค์นี้http://techno-terminal.blogspot.in/2015/08/convert-collection-into-comma-separated.html


0
static void Main(string[] args){          
List<string> listStrings = new List<string>() { "C#", "Asp.Net", "SQL Server", "PHP", "Angular" };  
string CommaSeparateString = GenerateCommaSeparateStringFromList(listStrings);  
Console.Write(CommaSeparateString);  
Console.ReadKey();}
private static string GenerateCommaSeparateStringFromList(List<string> listStrings){return String.Join(",", listStrings);}

แปลงรายการของสตริงเป็นสตริงที่คั่นด้วยเครื่องหมายจุลภาค C #


0

คุณยังสามารถแทนที่ ToString () ถ้ารายการของคุณมีมากกว่าหนึ่งสาย

public class ListItem
{

    public string string1 { get; set; }

    public string string2 { get; set; }

    public string string3 { get; set; }

    public override string ToString()
    {
        return string.Join(
        ","
        , string1 
        , string2 
        , string3);

    }

}

เพื่อรับสตริง csv:

ListItem item = new ListItem();
item.string1 = "string1";
item.string2 = "string2";
item.string3 = "string3";

List<ListItem> list = new List<ListItem>();
list.Add(item);

string strinCSV = (string.Join("\n", list.Select(x => x.ToString()).ToArray()));


0

คุณสามารถแยกรายการเอนทิตีด้วยเครื่องหมายจุลภาคเช่นนี้:

//phones is a list of PhoneModel
var phoneNumbers = phones.Select(m => m.PhoneNumber)    
                    .Aggregate(new StringBuilder(),
                        (current, next) => current.Append(next).Append(" , ")).ToString();

// Remove the trailing comma and space
if (phoneNumbers.Length > 1)
    phoneNumbers = phoneNumbers.Remove(phoneNumbers.Length - 2, 2);
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.