IT

쉼표로 구분 된 문자열로 목록 변환

lottoking 2020. 6. 22. 07:32
반응형

쉼표로 구분 된 문자열로 목록 변환


내 코드는 다음과 같습니다.

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>쉼표로 구분 된 문자열로 변환하는 가장 간단한 방법은 무엇입니까 ?


즐겨!

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

첫 번째 매개 변수 : ","
두 번째 매개 변수 :new List<uint> { 1, 2, 3, 4, 5 })

String.Join 은 두 번째 매개 변수로 목록을 가져 와서 첫 번째 매개 변수로 전달 된 문자열을 사용하여 모든 요소를 ​​단일 문자열로 결합합니다.


String.Join 메소드를 사용하여 항목을 결합 할 수 있습니다 .

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

사용 String.Join

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

사용 Linq Aggregation

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

이것을 따르십시오 :

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

정수 컬렉션이있는 경우 :

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

string.Join문자열을 얻는 데 사용할 수 있습니다 .

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

즐겨!


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

           }

MVC Razor View에서 쉼표로 구분 된 모든 역할을 평가하고 인쇄하는 데 사용했습니다.


.NET framework> 4.0을 사용하는 경우 String.Join을 사용할 수 있습니다.

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

목록에서 쉼표로 구분 된 문자열 배열을 가져 오려면 아래 예를 참조하십시오.

예:

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

시험

Console.WriteLine((string.Join(",", lst.Select(x=>x.ToString()).ToArray())));

HTH


We can try like this to separate list enties by comma

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

you can make use of google-collections.jar which has a utility class called Joiner

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

or

you can use StringUtils class which has function called join.To make use of StringUtils class,you need to use common-lang3.jar

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

for reference, refer this link http://techno-terminal.blogspot.in/2015/08/convert-collection-into-comma-separated.html


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

Convert a list of string to comma separated string C#


you can also override ToString() if your list item have more than one string

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

    }

}

to get csv string:

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

참고URL : https://stackoverflow.com/questions/14959824/convert-list-into-comma-separated-string

반응형