如何拆分()分隔字符串列表<字符串> [英] How to split() a delimited string to a List<String>

查看:151
本文介绍了如何拆分()分隔字符串列表<字符串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code:

    String[] lineElements;       
    . . .
    try
    {
        using (StreamReader sr = new StreamReader("TestFile.txt"))
        {
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                lineElements = line.Split(',');
                . . .

但后来想到也许我应该去同一个列表来代替。但是,这code:

but then thought I should maybe go with a List instead. But this code:

    List<String> listStrLineElements;
    . . .
    try
    {
        using (StreamReader sr = new StreamReader("TestFile.txt"))
        {
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                listStrLineElements = line.Split(',');
. . .

...给我,无法隐式转换类型字符串[]'到'System.Collections.Generic.List

推荐答案

string.Split()返回数组 - 你可以用它转换成一个列表了ToList()

string.Split() returns an array - you can convert it to a list using ToList():

listStrLineElements = line.Split(',').ToList();

这篇关于如何拆分()分隔字符串列表&LT;字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆