Convert List, string. A List can be converted to a string. This is possible with the ToArray method on the List type. We can also convert a string into a List.Conversions
The StringBuilder type helps with certain conversions, which are done with loops. When using StringBuilder, we must be careful with a trailing delimiter.
First example. We use the string.Join method to combine a List of strings into one string. The output can be used as a CSV record. On new .NET Framework versions, ToArray is not required.

However:In previous versions, we had to call ToArray on a List before using Join. In older programs this is still required.

List

Based on: .NET 4

C# program that converts List

using System;
using System.Collections.Generic; class Program
{
static void Main()
{
List<string> dogs = new List<string>();
dogs.Add("Aigi"); // Add string 1
dogs.Add("Spitz"); // 2
dogs.Add("Mastiff"); // 3
dogs.Add("Finnish Spitz"); // 4
dogs.Add("Briard"); // 5 string dogCsv = string.Join(",", dogs.ToArray());
Console.WriteLine(dogCsv);
}
} Output Aigi,Spitz,Mastiff,Finnish Spitz,Briard

Example 2. Here we use the StringBuilder class to convert a List to a single string. Note that you can convert a List of any object type into a string this way.StringBuilder

Final delimiter:The example has a final delimiter on the end. This is not present in code that uses string.Join. It can be inconvenient.

TrimEnd:Sometimes, it is good to remove the end delimiter with TrimEnd. Other times it is best left alone.

TrimEnd, TrimStart

C# program that uses List and StringBuilder

using System;
using System.Collections.Generic;
using System.Text; class Program
{
static void Main()
{
List<string> cats = new List<string>(); // Create new list of strings
cats.Add("Devon Rex"); // Add string 1
cats.Add("Manx"); // 2
cats.Add("Munchkin"); // 3
cats.Add("American Curl"); // 4
cats.Add("German Rex"); // 5 StringBuilder builder = new StringBuilder();
foreach (string cat in cats) // Loop through all strings
{
builder.Append(cat).Append("|"); // Append string to StringBuilder
}
string result = builder.ToString(); // Get string from StringBuilder
Console.WriteLine(result);
}
} Output Devon Rex|Manx|Munchkin|American Curl|German Rex|

Example 3. Here we convert a List of ints into a single string. The StringBuilder's Append method receives a variety of types. We can simply pass it the int.

And:Append() will handle the int on its own. It will convert it to a string and append it.

Performance:StringBuilder is fast for most programs. More speed could be acquired by using a char[] and then converting to a string.

Char Array

C# program that converts List types

using System;
using System.Collections.Generic;
using System.Text; class Program
{
static void Main()
{
List<int> safePrimes = new List<int>(); // Create list of ints
safePrimes.Add(5); // Element 1
safePrimes.Add(7); // Element 2
safePrimes.Add(11); // Element 3
safePrimes.Add(23); // Element 4 StringBuilder builder = new StringBuilder();
foreach (int safePrime in safePrimes)
{
// Append each int to the StringBuilder overload.
builder.Append(safePrime).Append(" ");
}
string result = builder.ToString();
Console.WriteLine(result);
}
} Output 5 7 11 23

Example 4. Finally, we get a List of strings from a string in CSV format. This requires the Split method. If you require per-item conversion, loop over the string array returned by Split.

C# program that converts string to List

using System;
using System.Collections.Generic; class Program
{
static void Main()
{
string csv = "one,two,three"; // The input string
string[] parts = csv.Split(','); // Call Split method
List<string> list = new List<string>(parts); // Use List constructor
foreach (string item in list)
{
Console.WriteLine(item);
}
}
} Output one
two
three

A summary. We converted Lists and strings using the string.Join methods and the StringBuilder approach. The List is easily concatenated and stored in a database or file with these methods.

如何将List<string>转化为string的更多相关文章

  1. Object 转化为String时的一个问题 null->"null"

    近日在工作出了一个较大的问题,导致被客户投诉. 事情大致是,某个功能里新增对用户手机的修改,在平台数据同步过程中,出现了将用户以前的要同步的数据,那时还没有手机号码所以是null,新功能上线后,将手机 ...

  2. Money类型转化为String去除小数点后0解决方法

    Money类型转化为String去除小数点后0从数据库提取Money类型后,字符串如:1212.0000 如何使其成为1212             注:去掉了小数点 如果是:1212.0100 使 ...

  3. mfc中CString转化为string的方法

    LL(1)分析法实验的mfc做到最后因为CString转化为string的问题卡了一个多小时,也是惨,网上各种方法找过都不行.幸亏最后还是找到几行代码搞定了.特此mark一下. USES_CONVER ...

  4. 将fastjson元素转化为String[]

    在fastjson中如果JSONObject中添加了 String[] 类型的元素 例如 JSONObject jo = new JSONObject(); String[] array = {&qu ...

  5. java String转int int转化为String

    String转int String str = "123"; int a = Integer.parseInt(str); System.out.println(a); Integ ...

  6. InputStream转化为String

    参考:https://blog.csdn.net/lmy86263/article/details/60479350 eg:  InputStream in = PropertiesUtils.cla ...

  7. C#中小数转化为string的问题

    在C#中,把小数转化为string, 比如 45.12, 转换为string时,应该就是"45.12" 但是在项目开发中,开发法国的branch时,由于culture使用的是FR- ...

  8. c和c++如何把一个整数转化为string

    c和c++如何把一个整数转化为string C++: 一.string转int的方式 采用最原始的string, 然后按照十进制的特点进行算术运算得到int,但是这种方式太麻烦,这里不介绍了. 采用标 ...

  9. Arduino 将 String 转化为 int

    Arduino 将 String 转化为 int 函数:toInt() 实例: String my_str = "; int my_int = my_str.toInt();

随机推荐

  1. Kruskal算法(二)之 C++详解

    本章是克鲁斯卡尔算法的C++实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3. 克鲁斯卡尔算法图解 4. 克鲁斯卡尔算法分析 5. 克鲁斯卡尔算法的代码说明 6. 克鲁斯卡尔算法的源码 转 ...

  2. 打开都是“Smart Adobe CC Blocker v1.0”已损坏,打不开。 您应该将它移到废纸篓。

    安全设置里允许任意来源打开就可以了 “系统偏好设置”->“安全性与隐私”->“允许从以下位置下载的应用程序”->任何来源.

  3. Bootflat – 基于 Bootstrap CSS 框架的扁平化界面

    Bootflat 是一个开源的扁平化的 UI 工具包,基于 Bootstrap 3.1.0 CSS 框架.它为 Web 开发人员提供了一个创建优雅的 Web 应用程序的更快,更容易和更少的重复任务的途 ...

  4. wordpress和普通网页如何使用百度分享组件

    百度分享组件是什么?摘自百度百科: 百度分享是一个提供网页地址收藏.分享及发送的WEB2.0按钮工具,网站的浏览者可以方便的分享到人人网.分享到开心网.分享到QQ空间.分享到新浪微博等一系列SNS站点 ...

  5. .net后台模拟浏览器get/post请求

    #region 后台模拟浏览器get/post请求 /// <summary> /// 发送请求方式 /// </summary> /// <param name=&qu ...

  6. 大M法(Big M Method)

    前面一篇讲的单纯形方法的实现,但程序输入的必须是已经有初始基本可行解的单纯形表. 但实际问题中很少有现成的基本可行解,比如以下这个问题: min f(x) = –3x1 +x2 + x3 s.t. x ...

  7. javascript设计模式之单体模式

    一入前端深似海,刚入前端,以为前端只是div+css布局外加jquery操作DOM树辣么简单.伴随着对前端学习的深入,发现前端也是博大精深,而且懂得越多,才发现自己越无知,所以一定不能停下脚步的学习. ...

  8. Microsoft Orleans 之 入门指南

    Microsoft Orleans 在.net用简单方法构建高并发.分布式的大型应用程序框架. 原文:http://dotnet.github.io/orleans/ 在线文档:http://dotn ...

  9. 创建html元素

    如果我要创建一个div元素. 1.使用DOM对象创建: 使用document.createElement('div')方法创建元素. 2.使用JQuery创建: 使用$('<div>通过J ...

  10. Java继承知识总结

    Java中的继承作为Java面向对象三大特性之一,在知识点上而言,还是很多的.现做出如下总结: 1. 继承的概念: 继承在本职上是特殊——一般的关系,即常说的is-a关系.子类继承父类,表明子类是一种 ...