C#对字符串进行处理时,经常需要进行String,String[]和List<String>之间的转换

本文分析一下它们的差异和转换

一.

  1. String > String[]

String s = "ab cd ef gh";
String[] sArray = s.Split(' ');

  2. String[] > String

string[] sArray = {"ab", "cd", "ef", "gh"};
string s = String.Join(" ", sArray);
//s = "ab cd ef gh";

  3.String[] > List<String>

string[] sArray = { "ab", "cd", "ef", "gh" };
List<String> list = new List<string>(sArray);

  4.List<String> > String[]

List<String> list = new List<string>();
list.Add("ab");
list.Add("cd");
list.Add("ef");
list.Add("gh");
string[] sArray = list.ToArray();

  5.String和List<String>之间的转换可以使用String[]来中转完成

二.

  1. String类型有很多常用的字符串操作成员

    

    字符串是不可变的,虽然这些方法看起来都会改变字符串对象,其实,它们不会改变而是返回了新的

    副本。对于一个String,任何“改变”都会分配一个新的恒定字符串。 

String s = "ab cd ef gh";
Console.WriteLine("{0}", s.ToUpper());
Console.WriteLine("{0}", s);
/*
返回结果:
AB CD EF GH
ab cd ef gh
*/

    2. String[]是定长的,String[]一般是在确定字符串数组的长度的情况下使用 

    3. List< String >一般在数组的长度会发生变化的情况下使用,例如在数组中间插入一个字符串

c# String ,String[] 和 List<String>之间的转换的更多相关文章

  1. String、Date、Calendar之间的转换

    1.String.Date.Calendar之间的转换 要用到格式化类SimpleDateFormat package com.rong.se; import java.text.ParseExcep ...

  2. c#-WPF string,color,brush之间的转换

    原文:c#-WPF string,color,brush之间的转换 String转换成Color string-"ffffff" Color color = (Color)Colo ...

  3. String类型和基本数据类型之间的转换

    Java 中基本类型和字符串之间的转换 在程序开发中,我们经常需要在基本数据类型和字符串之间进行转换. 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使 ...

  4. 将String转化成Stream,将Stream转换成String, C# Stream 和 byte[] 之间的转换(文件流的应用)

    static void Main( string[] args ) { string str = "Testing 1-2-3"; //convert string 2 strea ...

  5. char*,string,char a[], const char *,之间的转换

    1. const char* 和string 转换 (1) const char*转换为 string,直接赋值即可.      EX: const char* tmp = "tsinghu ...

  6. [java]String和Date、Timestamp之间的转换

    一.String与Date(java.util.Date)互转  1.1 String -> Date Date date = DateFormat.parse(String  str); St ...

  7. 【Java】String和Date、Timestamp之间的转换

    首先,定义一个Format的日期格式: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 一.S ...

  8. C#中的Byte,String,Int,Hex之间的转换函数。

    /// <summary> Convert a string of hex digits (ex: E4 CA B2) to a byte array. </summary> ...

  9. String,int,Integer之间的转换

    public class Test{ public static void main(String[] args) { //int转换成Integer Integer in = new Integer ...

  10. String和Date、Timestamp之间的转换

    一.String与Date(java.util.Date)互转 1.1 String -> Date String dateStr = "2010/05/04 12:34:23&quo ...

随机推荐

  1. selenium+python+eclipse 实现 “问卷星”网站,登录与检查登录示例!

    1.使用selenium+python+eclipse实现的登录"问卷星",问卷星访问地址:https://www.sojump.com/ 2.实现步骤:1)进入链接---首页-- ...

  2. Spring Boot之WebSocket

    一.项目说明 1.项目地址:https://github.com/hqzmss/test01-springboot-websocket.git 2.IDE:IntelliJ IDEA 2018.1.1 ...

  3. [asp.net mvc 奇淫巧技] 06 - 也许你的项目同一个用户的请求都是同步的

    一.感慨 很久前看到一篇博客中有句话大致的意思是:“asp.net 程序性能低下的主要原因是开发人员技术参差不齐”,当时看到这句话不以为然,然而时间过的越久接触的.net 开发人员越多就越认同这句话: ...

  4. .net core 注入机制与Autofac

    本来是要先出注入机制再出 管道 的,哈哈哈……就是不按计划来…… 这里扯扯题外话:为什么要注入(DI,dependency-injection),而不用 new 对象? 可能我们都很清楚,new 对象 ...

  5. Nginx+Keepalived 集群方案

    1.Keepalived高可用软件 Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,kee ...

  6. Vue.js-10:第十章 - 组件间的数据通信

    一.前言 在上一章的学习中,我们继续学习了 Vue 中组件的相关知识,了解了在 Vue 中如何使用组件的 data.prop 选项.在之前的学习中有提到过,组件是 Vue 中的一个非常重要的概念,我们 ...

  7. asp.net core系列 55 IS4结合Identity密码保护API

    一.概述 OAuth 2.资源所有者密码授权允许客户端(Client项目)向令牌服务(IdentityServer项目)发送用户名和密码,并获取代表该用户的访问令牌.本篇将IS4结合asp.net c ...

  8. Java进阶篇设计模式之七 ----- 享元模式和代理模式

    前言 在上一篇中我们学习了结构型模式的组合模式和过滤器模式.本篇则来学习下结构型模式最后的两个模式, 享元模式和代理模式. 享元模式 简介 享元模式主要用于减少创建对象的数量,以减少内存占用和提高性能 ...

  9. 分享自己写的一个.net方法缓存源码

    在服务器性能优化中,我们更多的是要考虑到缓存的使用,分享一个自己编写的方法缓存的框架,使用非常方便.话不多说,先上使用例子: 1.定义要使用缓存的类及方法: public class Example ...

  10. 递归一个List<T>,可自己根据需要改造为通用型。

    /// <summary> /// 递归一个List<ProvinceOrg> /// </summary> /// <returns></ret ...