C#中List〈string〉和string[]数组之间的相互转换_C#教程

1,从System.String[]转到List<System.String>

System.String[] str={"str","string","abc"};

List<System.String> listS=new List<System.String>(str);

 

2, 从List<System.String>转到System.String[]

List<System.String> listS=new List<System.String>();

listS.Add("str");

listS.Add("hello");

System.String[] str=listS.ToArray();

 

测试如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.String[] sA = { "str","string1","sting2","abc"};
            List<System.String> sL = new List<System.String>();
            for (System.Int32 i = 0; i < sA.Length;i++ )
            {
                Console.WriteLine("sA[{0}]={1}",i,sA[i]);
            }
            sL = new List<System.String>(sA);
            sL.Add("Hello!");
            foreach(System.String s in sL)
            {
                Console.WriteLine(s);
            }
            System.String[] nextString = sL.ToArray();
            Console.WriteLine("The Length of nextString is {0}",nextString.Length);
            Console.Read();
        }
    }
}

结果显示:

时间: 2024-09-17 04:47:42

C#中List〈string〉和string[]数组之间的相互转换_C#教程的相关文章

string类的使用方法详解_C#教程

String:字符串类型 1.构造函数. String() :构造一个空字符串对象.String(byte[] bytes) :通过byte数组构造字符串对象.String(byte[] bytes, int offset, int length) :通过byte数组,从offset开始,总共length长的字节构造字符串对象.String(char[] value) :通过char数组构造字符串对象.String(char[] value, int offset, int count) :通过

C#中使用基数排序算法对字符串进行排序的示例_C#教程

开始之前 假设最长字符串的长度是L,以L作为输入的长度, 然后假定所有的字符串都"补齐"到此长度,这个补齐只是逻辑上的,我们可以假想有一种"空字符", 它小于任何其它字符,用此字符补齐所有长度不足的字符串.例如:最长的字符串长度为9,有一个字符串A长度为6, 那么当比较第7位字符的时候,我们让A[7]为"空字符". 如果要包含所有的字符似乎并不容易,我们先定义一个字符集, 待排序字符串中的所有字符都包含在这个字符集里 //字符集 private

C#中DataTable 转换为 Json的方法汇总(三种方法)_C#教程

在web开发中,我们可能会有这样的需求,为了便于前台的JS的处理,我们需要将查询出的数据源格式比如:List<T>.DataTable转换为Json格式.特别在使用Extjs框架的时候,Ajax异步请求的数据格式就是Json.鉴于此,我今天来分享将DataTable 转换成 Json的3种方法.换句话说如何在ASP.NET将一个DataTable序列化为 Json数组.或者如何从一个DataTable返回一个Json字符串.这篇文章将采用StringBuilder,JavaScriptSeri

C#中实现Json序列化与反序列化的几种方式_C#教程

什么是JSON? JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and easy for machines to parse and generate. JSON is a text format that is completely language independent. 翻译:Json[javascrip

C#中使用split分割字符串的几种方法小结_C#教程

第一种方法: 复制代码 代码如下: string s=abcdeabcdeabcde;string[] sArray=s.Split(c) ;foreach(string i in sArray)Console.WriteLine(i.ToString()); 输出下面的结果:abdeabdeabde 第二种方法: 我们看到了结果是以一个指定的字符进行的分割.使用另一种构造方法对多个字符进行分割: 复制代码 代码如下: string s=abcdeabcdeabcdestring[] sArra

深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换_C 语言

复制代码 代码如下:     #ifndef USE_H_      #define USE_H_      #include <iostream>      #include <windows.h>      #include <string>      using namespace std;      class CUser      {      public:          CUser();          virtual~ CUser();      

C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换_C#教程

定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 复制代码 代码如下: (1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串"); (2)byte[] bt=Convert.FromBase64String("字符串"); 2.字符串转流 复制代码 代码如下: (1)MemoryStream ms=new MemoryStream(System.Text.Encoding.

wchar_t,char,string,wstring之间的相互转换_C 语言

在处理中文时有时需要进行wchar_t,char,string,wstring之间的转换. 其中char和string之间.wchar_t和wstring之间的转换较为简单,代码在vs2010下测试通过. 复制代码 代码如下: #include <iostream>#include <string>#include <tchar.h>#include <Windows.h> using namespace std; //Converting a WChar 

.net中前台javascript与后台c#函数相互调用问题_C#教程

C#代码与javaScript函数的相互调用 问: 1.如何在JavaScript访问C#函数? 2.如何在JavaScript访问C#变量? 3.如何在C#中访问JavaScript的已有变量? 4.如何在C#中访问JavaScript函数? 问题1答案如下: javaScript函数中执行C#代码中的函数: 方法一:1.首先建立一个按钮,在后台将调用或处理的内容写入button_click中;         2.在前台写一个js函数,内容为document.getElementById("