• list 快速操作字符串


    1、 LinkedList的使用

    https://www.cnblogs.com/ChengWenHao/p/CSharpLinkedList.html

    private static void TestLinkList()
    {
    LinkedList<Person> linkListPerson = new LinkedList<Person>();
    Person p = null;
    for (int i = 1; i < 10; i++)
    {
    p = new Person($"程序员{i}", i + 18,i%5==1?"女":"男");
    //添加
    linkListPerson.AddLast(p);
    //linkListPerson.AddFirst(p);
    }

    Console.WriteLine($"新增的总人数:{linkListPerson.Count}");
    Console.WriteLine("-------------------------------------------------------------");


    //遍历
    LinkedListNode<Person> linkNodePerson = linkListPerson.First;
    linkNodePerson.Value.SayHi();

    while (linkNodePerson.Next!=null)
    {
    linkNodePerson = linkNodePerson.Next;
    linkNodePerson.Value.SayHi();
    }

    Console.WriteLine("-------------------------------------------------------------");

    //删除
    while (linkNodePerson.Value != null && linkListPerson.Count > 0)
    {
    linkNodePerson = linkListPerson.Last;
    Console.Write($"当前总人数:{linkListPerson.Count}, 即将移除:{linkNodePerson.Value.Name} --> ");
    linkListPerson.RemoveLast();
    Console.WriteLine($"移除后总人数:{linkListPerson.Count}");
    }

    }

    2、

     使用List泛型读取和保存文本文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using System.Management;//添加引用
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //测试代码: 
                test mgr = new test();
                mgr.WriteListToTextFile(mgr.GetUserNames(), @"c:\test.txt"); //测试生成新的Txt文件
                List<string> list = mgr.ReadTextFileToList(@"C:\test.txt");//记取字符串 
                foreach (string s in list) Console.WriteLine(s); //显示出来′ 
                Console.ReadKey(); //按′任一键关闭Console 
             
                /*
                 * 结果:
                 *  JUQI\ASPNET
                    JUQI\capable
                    JUQI\Guest
                    JUQI\itd0300166
                    JUQI\IUSR_PLSH166
                    JUQI\IWAM_PLSH166
                    JUQI\SUPPORT_388945a0
                 * 
                 * */
    
            }
        }
            public class test
            {
    
                /// <summary>
                /// 获取本机上的用户帐户
                /// </summary>
                /// <param name="domainName"></param>
                /// <returns></returns>
                public  List<String> GetUserNames()
                {
                    ManagementClass mcs = new ManagementClass("Win32_UserAccount");
                    ManagementObjectCollection moc = mcs.GetInstances();
                    List<String> lstNames = new List<string>();
                    foreach (ManagementObject mo in moc)
                    {
                        lstNames.Add(Environment.UserDomainName + "\\" + mo.GetPropertyValue("Name").ToString());
                        //domainName = mo.GetPropertyValue("Domain").ToString();
                    }
                    return lstNames;
                }
    
    
                //将List转换为TXT文件
                public void WriteListToTextFile(List<string> list, string txtFile)
                {
                    //创建一个文件流,用以写入或者创建一个StreamWriter 
                    FileStream fs = new FileStream(txtFile, FileMode.OpenOrCreate, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Flush();
                    // 使用StreamWriter来往文件中写入内容 
                    sw.BaseStream.Seek(0, SeekOrigin.Begin);
                    for (int i = 0; i < list.Count; i++) sw.WriteLine(list[i]);
                    //关闭此文件t 
                    sw.Flush();
                    sw.Close();
                    fs.Close();
                }
    
    
                //读取文本文件转换为List 
                public List<string> ReadTextFileToList(string fileName)
                {
                    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    List<string> list = new List<string>();
                    StreamReader sr = new StreamReader(fs);
                    //使用StreamReader类来读取文件 
                    sr.BaseStream.Seek(0, SeekOrigin.Begin);
                    // 从数据流中读取每一行,直到文件的最后一行
                    string tmp = sr.ReadLine();
                    while (tmp != null)
                    {
                        list.Add(tmp);
                        tmp = sr.ReadLine();
                    }
                    //关闭此StreamReader对象 
                    sr.Close();
                    fs.Close();
                    return list;
                }
            }
    
        }
  • 相关阅读:
    C#中 ??、 ?、 ?: 、?.、?[ ] 问号各组合含义
    ASP.NET Core MVC配置差异(3.0和2.X)
    vs code搭建Django环境
    解决真机编译出现System.DllNotFoundException: 'libmono-native.so'错误都方法
    选择器
    Web.Config配置
    读Xamarin文档记录
    【前端自动化】Gulp的使用(一):安装gulp
    关于angularJS绑定数据时自动转义html标签
    【记录】两年程序生涯的点滴与反思
  • 原文地址:https://www.cnblogs.com/chinayixia/p/13224070.html
Copyright © 2020-2023  润新知