要将字符串 str="1;2;3" 转换为 List strlist={1,2,3},可以使用 Split 方法将字符串分割为多个子字符串,然后将这些子字符串添加到 List 中。\n\n以下是实现这个转换的示例代码:\n\ncsharp\nusing System;\nusing System.Collections.Generic;\n\nclass Program\n{\n static void Main()\n {\n string str = "1;2;3";\n List<string> strlist = new List<string>(str.Split(';'));\n \n foreach (string s in strlist)\n {\n Console.WriteLine(s);\n }\n }\n}\n\n\n输出结果为:\n\n\n1\n2\n3\n\n\n在上面的示例中,首先使用 Split 方法将字符串 str 分割为多个子字符串。Split 方法使用分号 ';' 作为分隔符,返回一个字符串数组。然后,我们将该字符串数组传递给 List 的构造函数,以创建一个包含这些子字符串的 List 对象。

C# 字符串转换为列表:将 {1, 2, 3}" title="C# 字符串转换为列表:将 "1;2;3" 转换为 List {1, 2, 3}" loading="lazy">

原文地址: https://www.cveoy.top/t/topic/pZln 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录