将字符串数组拆分为锯齿状对象数组
split a string array to a jagged object array
我想创建一个字符串数组,其中包含名称值和一些数字(字符串)
我想将它们传递给一个函数,该函数将获取数组,然后将它们拆分为对象锯齿状数组(1 个字符串数组和 1 个整数数组)
数组是:
string[] str= { "toto", "the", "moto", "my", "friend","12","13","14","99","88"};
函数如下所示:
public object[][] bloop (string[] bstr)
{
}
下一步是什么?
您的方案看起来设计不佳,可能会导致错误和性能问题。更好的方法是更改代码以使用通用 List<>
或类似的东西。但是在您当前的问题中,您可以使用以下代码:
public object[][] bloop (string[] bstr)
{
var numbers = new List<int>();
var strings = new List<string>();
var result = new object[2][];
foreach(var str in bstr)
{
int number = 0;
if(int.TryParse(str, out number))
{
numbers.Add(number);
}
else
{
strings.Add(str);
}
}
result[0] = strings.ToArray();
result[1] = numbers.ToArray();
return result;
}
我同意你的要求听起来很奇怪,应该用不同的方法来解决。然而,这会做你想做的事:
public T[][] Bloop<T>(T[] items)
{
if (items == null) throw new ArgumentNullException("items");
if (items.Length == 1) return new T[][] { items, new T[] { } };
int firstLength = (int) Math.Ceiling((double)items.Length / 2);
T[] firstPart = new T[firstLength];
Array.Copy(items, 0, firstPart, 0, firstLength);
int secondLength = (int)Math.Floor((double)items.Length / 2);
T[] secondPart = new T[secondLength];
Array.Copy(items, firstLength, secondPart, 0, secondLength);
return new T[][] { firstPart, secondPart };
}
您的样本:
string[] str= { "toto", "the", "moto", "my", "friend","12","13","14","99","88"};
string[][] result = Bloop(str);
如果您需要第二个数组作为 int[]
您可以使用以下内容:
int[] ints = Array.ConvertAll(result[1], int.Parse);
Linq解决方法。
您有 两组 :第一个包含可以解析为 int
的项,第二组包含所有其他项,因此 GroupBy
看起来很自然:
public Object[][] bloop(string[] bstr) {
if (null == bstr)
throw new ArgumentNullException("bstr");
int v;
return bstr
.GroupBy(x => int.TryParse(x, out v))
.OrderBy(chunk => chunk.Key) // let strings be the first
.Select(chunk => chunk.ToArray())
.ToArray();
}
测试:
string[] str = { "toto", "the", "moto", "my", "friend", "12", "13", "14", "99", "88" };
// toto, the, moto, my, friend
// 12, 13, 14, 99, 88
Console.Write(String.Join(Environment.NewLine,
bloop(str).Select(x => String.Join(", ", x))));
public static object[][] bloop(string[] bstr)
{
object[][] result = new object[2][] { new object[bstr.Length], new object[bstr.Length] };
int sFlag = 0, iFlag = 0, val;
foreach (string str in bstr)
if (int.TryParse(str, out val))
result[1][iFlag++] = val;
else
result[0][sFlag++] = str;
return result;
}
我想创建一个字符串数组,其中包含名称值和一些数字(字符串)
我想将它们传递给一个函数,该函数将获取数组,然后将它们拆分为对象锯齿状数组(1 个字符串数组和 1 个整数数组)
数组是:
string[] str= { "toto", "the", "moto", "my", "friend","12","13","14","99","88"};
函数如下所示:
public object[][] bloop (string[] bstr)
{
}
下一步是什么?
您的方案看起来设计不佳,可能会导致错误和性能问题。更好的方法是更改代码以使用通用 List<>
或类似的东西。但是在您当前的问题中,您可以使用以下代码:
public object[][] bloop (string[] bstr)
{
var numbers = new List<int>();
var strings = new List<string>();
var result = new object[2][];
foreach(var str in bstr)
{
int number = 0;
if(int.TryParse(str, out number))
{
numbers.Add(number);
}
else
{
strings.Add(str);
}
}
result[0] = strings.ToArray();
result[1] = numbers.ToArray();
return result;
}
我同意你的要求听起来很奇怪,应该用不同的方法来解决。然而,这会做你想做的事:
public T[][] Bloop<T>(T[] items)
{
if (items == null) throw new ArgumentNullException("items");
if (items.Length == 1) return new T[][] { items, new T[] { } };
int firstLength = (int) Math.Ceiling((double)items.Length / 2);
T[] firstPart = new T[firstLength];
Array.Copy(items, 0, firstPart, 0, firstLength);
int secondLength = (int)Math.Floor((double)items.Length / 2);
T[] secondPart = new T[secondLength];
Array.Copy(items, firstLength, secondPart, 0, secondLength);
return new T[][] { firstPart, secondPart };
}
您的样本:
string[] str= { "toto", "the", "moto", "my", "friend","12","13","14","99","88"};
string[][] result = Bloop(str);
如果您需要第二个数组作为 int[]
您可以使用以下内容:
int[] ints = Array.ConvertAll(result[1], int.Parse);
Linq解决方法。
您有 两组 :第一个包含可以解析为 int
的项,第二组包含所有其他项,因此 GroupBy
看起来很自然:
public Object[][] bloop(string[] bstr) {
if (null == bstr)
throw new ArgumentNullException("bstr");
int v;
return bstr
.GroupBy(x => int.TryParse(x, out v))
.OrderBy(chunk => chunk.Key) // let strings be the first
.Select(chunk => chunk.ToArray())
.ToArray();
}
测试:
string[] str = { "toto", "the", "moto", "my", "friend", "12", "13", "14", "99", "88" };
// toto, the, moto, my, friend
// 12, 13, 14, 99, 88
Console.Write(String.Join(Environment.NewLine,
bloop(str).Select(x => String.Join(", ", x))));
public static object[][] bloop(string[] bstr)
{
object[][] result = new object[2][] { new object[bstr.Length], new object[bstr.Length] };
int sFlag = 0, iFlag = 0, val;
foreach (string str in bstr)
if (int.TryParse(str, out val))
result[1][iFlag++] = val;
else
result[0][sFlag++] = str;
return result;
}