.net 数组与字符串、集合之间互转
1、数组与字符串互转
1 2 3 4 | string str = "1,2,3,4,5,6,7" ; string [] strArray = str.Split( ',' ); //字符串转数组 str = string .Empty; str = string .Join( "," , strArray); //数组转成字符串 |
2、声明数组. 第一种方法. 声明并分配元素大小.
1 2 3 4 | int [] Myint = new int [30]; Myint[0] = 30; Myint[1] = 50; // 以此类推, 起始下标为0 |
3、声明数组,第二种方法, 声明并直接赋值,没有指定元素大小.
1 | int [] Myint1 = { 20,10,50,65,18,90}; |
4、声明数组,第三种方法, 声明并分配大小,且赋值.
1 | int [] i = new int [5] { 10, 20, 30, 40, 50 }; |
5、长度确定的数组取值
1 2 3 4 5 6 7 8 9 10 11 12 13 | // foreach循环遍历数组.. int [] Sum = new int [50]; Random rd = new Random(); // 先用for循环给数组取随机数. for ( int s = 0; s <= Sum.Length - 1; s++) // Sum.Length是数组的一个属性,Length代表数组的长度 { Sum[s] = rd.Next(100); } // 遍历数组输出 foreach ( int t in Sum) { Console.WriteLine(t); } |
6、数组与集合互转
1 2 3 4 5 6 7 8 9 10 | //从System.String[]转到List<System.String> System.String[] str={ "str" , "string" , "abc" }; List<System.String> listS= new List<System.String>(str); //从List<System.String>转到System.String[] List<System.String> listS= new List<System.String>(); listS.Add( "str" ); listS.Add( "hello" ); System.String[] str=listS.ToArray(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现