string tags = "9,3,12,43,2"
List<int> TagIds = tags.Split(',');
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为split方法返回一个字符串[]
请帮忙.
给出以下字符串:
string str = "1,2,3";
Run Code Online (Sandbox Code Playgroud)
这是将它转换为int数组的最佳扩展吗?
static class StringExtensions
{
public static int[] ToIntArray(this string s)
{
return ToIntArray(s, ',');
}
public static int[] ToIntArray(this string s, char separator)
{
string[] ar = s.Split(separator);
List<int> ints = new List<int>();
foreach (var item in ar)
{
int v;
if (int.TryParse(item, out v))
ints.Add(v);
}
return ints.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud) 特定
IList<int> indexes;
ICollection<T> collection;
Run Code Online (Sandbox Code Playgroud)
什么是最优雅的方式来提取所有牛逼在收集基础上,提供的索引的索引?
例如,如果包含集合
"Brian", "Cleveland", "Joe", "Glenn", "Mort"
Run Code Online (Sandbox Code Playgroud)
并包含索引
1, 3
Run Code Online (Sandbox Code Playgroud)
回报将是
"Cleveland," "Glenn"
Run Code Online (Sandbox Code Playgroud)
编辑:假设索引始终按升序排序.
当我生成逗号分隔列表时,我讨厌如何删除尾随逗号.
有没有更好的办法?我经常这样做,所以寻找意见.
for(int x = 0; x < list.Count; x++)
{
sb.Append(list[x].Name);
sb.Append(",");
}
var result = sb.toString().Substring(0, result.length - 2);
Run Code Online (Sandbox Code Playgroud) 我正在使用C#.
我有一个字符串数组如下:"1,2,3,4,5,..."
我正在尝试将字符串数组转换为字节数组,如下所示[]{1,2,3,4,5,...}
:最好的方法是什么?
谢谢.
我有一个字符串:
string test = "1,2,3,4";
有没有更简单的方法(语法上)将它转换为List<int>等效于这样的东西:
string[] testsplit = test.Split(',');
List<int> intTest = new List<int>();
foreach(string s in testsplit)
intTest.Add(int.Parse(s));
Run Code Online (Sandbox Code Playgroud) 我在C#中有一个字符串.它在开始时是空白的,但最终会变成类似的东西
public string info12 = "0, 50, 120, 10";
Run Code Online (Sandbox Code Playgroud)
你们其中一个人可能在想,是吗?是不是整数数组?那么它需要暂时保持一个字符串,它必须是一个字符串.
如何将此字符串转换为字符串数组(变量info13),以便最终将其引用到更多变量中.
info 14 = info13[0];
info 15 = info13[1];
Run Code Online (Sandbox Code Playgroud)
请注意:这不是一个重复的问题.如果你读完整件事,我清楚地说我有一个字符串数组而不是整数.
如何将字符串var numbers = "2016, 2017, 2018";转换为List<int>?
我试过这个:
List<int> years = Int32.Parse(yearsString.Split(',')).ToList();
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误消息:
无法从string []转换为字符串.