如何将List<string>转化为string
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.
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.
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.
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的更多相关文章
- Object 转化为String时的一个问题 null->"null"
近日在工作出了一个较大的问题,导致被客户投诉. 事情大致是,某个功能里新增对用户手机的修改,在平台数据同步过程中,出现了将用户以前的要同步的数据,那时还没有手机号码所以是null,新功能上线后,将手机 ...
- Money类型转化为String去除小数点后0解决方法
Money类型转化为String去除小数点后0从数据库提取Money类型后,字符串如:1212.0000 如何使其成为1212 注:去掉了小数点 如果是:1212.0100 使 ...
- mfc中CString转化为string的方法
LL(1)分析法实验的mfc做到最后因为CString转化为string的问题卡了一个多小时,也是惨,网上各种方法找过都不行.幸亏最后还是找到几行代码搞定了.特此mark一下. USES_CONVER ...
- 将fastjson元素转化为String[]
在fastjson中如果JSONObject中添加了 String[] 类型的元素 例如 JSONObject jo = new JSONObject(); String[] array = {&qu ...
- java String转int int转化为String
String转int String str = "123"; int a = Integer.parseInt(str); System.out.println(a); Integ ...
- InputStream转化为String
参考:https://blog.csdn.net/lmy86263/article/details/60479350 eg: InputStream in = PropertiesUtils.cla ...
- C#中小数转化为string的问题
在C#中,把小数转化为string, 比如 45.12, 转换为string时,应该就是"45.12" 但是在项目开发中,开发法国的branch时,由于culture使用的是FR- ...
- c和c++如何把一个整数转化为string
c和c++如何把一个整数转化为string C++: 一.string转int的方式 采用最原始的string, 然后按照十进制的特点进行算术运算得到int,但是这种方式太麻烦,这里不介绍了. 采用标 ...
- Arduino 将 String 转化为 int
Arduino 将 String 转化为 int 函数:toInt() 实例: String my_str = "; int my_int = my_str.toInt();
随机推荐
- Ext.grid.CheckboxSelectionModel状态设置
直接上代码: var model = grid.getSelectionModel(); model.selectAll();//选择所有行 model.selectFirstRow();//选择第一 ...
- 为网格布局图片打造的超炫 CSS 加载动画
今天,我想与大家分享一些专门为网格布局的图像制作的很酷的 CSS 加载动画效果.您可以把这些效果用在你的作品集,博客或任何你想要的网页中.设置很简单.我们使用了下面这些工具库来实现这个效果: Norm ...
- 各种排序学习归纳总结(Java)
排序总结 根据<数据结构与算法分析——Java语言描述><INTRODUCTION TO JAVA PROGRAMMING>.维基及各技术博客知识点来总结的. 如果刚入门学习 ...
- java中String的相等比较
首先贴出测试用例: package test; import org.junit.Test; /** * Created by Administrator on 2015/9/16. * */ pub ...
- 意译:《JVM Internals》
译者语 为加深对JVM的了解和日后查阅时更方便,于是对原文进行翻译.内容是建立在我对JVM的认识的基础上翻译的,加上本人的英语水平有限,若有纰漏请大家指正,谢谢. 原文地址:http://blog.j ...
- 【Swift学习】Swift编程之旅(二)
在本节将介绍一些最基础的知识 swift提供自己版本的类型,下面说明几种简单的类型 Int 整型 Double和float 浮点型 String 字符串型 Bool 布尔型 它也提供了3种主要的强大的 ...
- 九、SDP
1. SDP 1.1 服务概述 SDP, Service Discovery Protocol,服务发现协议. 1.1.1 概念 SDP提供了一种用于发现服务及这些可用服务 ...
- div模拟textarea实现高度自增长
今天突然有位前端的朋友问我textarea怎么实现高度随内容自增长,我一下子懵了,首先想到的是用js改变textarea的高度,但是百度了很多参考代码效果都不是很理想. 因为之前实际项目中用的text ...
- JQuery02
一:JQuery知识点 *:JQuery的dom操作 *:动态创建dom节点 比如动态创建表格等,在js里面进行完成. *删除节点 这里面的删除就是将其放在了一个地方,并不是真的删除,之后可以使用. ...
- 【转】 CSS3实现10种Loading效果
昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… PS:如需转载,请注明出处! 第1种效果: 代码如下: <div class="l ...