如何使用特定字符将数组拆分为更小的数组?
How do I split an array into smaller arrays using a specific character?
如何使用特定字符拆分字符串数组?此示例使用 '@':
string [] stringArray = new string [10];
stringArray[0] = "Hi there, this is page one" //goes into new arrayA
stringArray[1] = "Hi there, this is page two" //goes into new arrayA
stringArray[2] = "Hi there, this is page three" //goes into new arrayA
stringArray[3] = "@" //split
stringArray[4] = "New book, page one" //goes into new arrayB
stringArray[5] = "New book, page two" //goes into new arrayB
您可以编写一个使用 Skip
和 TakeWhile
的扩展方法。
此解决方案是通用的,这意味着它适用于您提供的任何类型。请注意,对于引用类型,将进行值比较而不进行引用比较。
public static List<List<T>> Split<T>(this List<T> array, T seperator)
{
var currentIndex = 0;
var splitedList = new List<List<T>>();
while (currentIndex < array.Count)
{
var part = array.Skip(currentIndex).TakeWhile(item => !item.Equals(seperator)).ToList();
splitedList.Add(part);
currentIndex += part.Count + 1;
}
return splitedList;
}
string[] stringArray = new string[6];
stringArray[0] = "Hi there, this is page one"; //goes into new arrayA
stringArray[1] = "Hi there, this is page two"; //goes into new arrayA
stringArray[2] = "Hi there, this is page three"; //goes into new arrayA
stringArray[3] = "@"; //split
stringArray[4] = "New book, page one"; //goes into new arrayB
stringArray[5] = "New book, page two"; //goes into new arrayB
var splittedValue = stringArray.ToList().Split("@");
我知道你有一个很大的列表,你想做一个像拆分这样的流,你可以使用 yield return
。这样做的好处是,当读取列表中的下一项时,代码只会执行到下一个 yield return
语句。
public static IEnumerable<IList<T>> Split<T>(this IEnumerable<T> collection, T seperator)
{
var items = new List<T>();
foreach (var item in collection)
{
if (item.Equals(seperator))
{
yield return items;
items = new List<T>();
}
else items.Add(item);
}
yield return items;
}
如果数组的大小不够大(数千项),您可以连接字符串,然后再拆分它们:
string new_string = String.Join("#", stringArray);
string[] combined_array = new_string.Split("@".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string[] new_array_1 = combined_array[0].Split("#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string[] new_array_2 = combined_array[1].Split("#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
combined_array 中的项目数取决于“@”的数量。所以一般来说,您可以遍历 combined_array 并获得单独的数组。
应该这样做:
public IEnumerable<IEnumerable<T>> Split<T>(IEnumerable<T> input, T splitOn)
{
var l = new List<T>();
foreach (var item in input)
{
if(object.Equals(item, splitOn))
{
yield return l;
l = new List<T>();
}
else
l.Add(item);
}
yield return l;
}
另一种方法,使用带有 Skip
、Take
和 yield return
的扩展方法进行延迟加载。在第二次编辑中,我让它使用泛型来拆分所有类型的 IEnumerables
:
public static IEnumerable<IEnumerable<T>> SplitArray<T>(this IEnumerable<T> s,T splitItem)
{
// First we get all the indexes where the string is found,
// adding the last index of the array
var indexes = s.Select((b, i) => b.Equals(splitItem) ? i : -1).Where(i => i != -1)
.Union(new int[] { s.Count() }).ToArray();
int skip = 0; //variable to know where the next chunk starts
foreach (int index in indexes)
{
IEnumerable<T> array = s.Skip(skip).Take(index - skip).ToArray();
yield return array; //we return the chunk
skip = index+1;
}
}
用法:
foreach(var splitted in stringArray.SplitArray("@"))
{
//splited is a string[]
}
如何使用特定字符拆分字符串数组?此示例使用 '@':
string [] stringArray = new string [10];
stringArray[0] = "Hi there, this is page one" //goes into new arrayA
stringArray[1] = "Hi there, this is page two" //goes into new arrayA
stringArray[2] = "Hi there, this is page three" //goes into new arrayA
stringArray[3] = "@" //split
stringArray[4] = "New book, page one" //goes into new arrayB
stringArray[5] = "New book, page two" //goes into new arrayB
您可以编写一个使用 Skip
和 TakeWhile
的扩展方法。
此解决方案是通用的,这意味着它适用于您提供的任何类型。请注意,对于引用类型,将进行值比较而不进行引用比较。
public static List<List<T>> Split<T>(this List<T> array, T seperator)
{
var currentIndex = 0;
var splitedList = new List<List<T>>();
while (currentIndex < array.Count)
{
var part = array.Skip(currentIndex).TakeWhile(item => !item.Equals(seperator)).ToList();
splitedList.Add(part);
currentIndex += part.Count + 1;
}
return splitedList;
}
string[] stringArray = new string[6];
stringArray[0] = "Hi there, this is page one"; //goes into new arrayA
stringArray[1] = "Hi there, this is page two"; //goes into new arrayA
stringArray[2] = "Hi there, this is page three"; //goes into new arrayA
stringArray[3] = "@"; //split
stringArray[4] = "New book, page one"; //goes into new arrayB
stringArray[5] = "New book, page two"; //goes into new arrayB
var splittedValue = stringArray.ToList().Split("@");
我知道你有一个很大的列表,你想做一个像拆分这样的流,你可以使用 yield return
。这样做的好处是,当读取列表中的下一项时,代码只会执行到下一个 yield return
语句。
public static IEnumerable<IList<T>> Split<T>(this IEnumerable<T> collection, T seperator)
{
var items = new List<T>();
foreach (var item in collection)
{
if (item.Equals(seperator))
{
yield return items;
items = new List<T>();
}
else items.Add(item);
}
yield return items;
}
如果数组的大小不够大(数千项),您可以连接字符串,然后再拆分它们:
string new_string = String.Join("#", stringArray);
string[] combined_array = new_string.Split("@".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string[] new_array_1 = combined_array[0].Split("#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string[] new_array_2 = combined_array[1].Split("#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
combined_array 中的项目数取决于“@”的数量。所以一般来说,您可以遍历 combined_array 并获得单独的数组。
应该这样做:
public IEnumerable<IEnumerable<T>> Split<T>(IEnumerable<T> input, T splitOn)
{
var l = new List<T>();
foreach (var item in input)
{
if(object.Equals(item, splitOn))
{
yield return l;
l = new List<T>();
}
else
l.Add(item);
}
yield return l;
}
另一种方法,使用带有 Skip
、Take
和 yield return
的扩展方法进行延迟加载。在第二次编辑中,我让它使用泛型来拆分所有类型的 IEnumerables
:
public static IEnumerable<IEnumerable<T>> SplitArray<T>(this IEnumerable<T> s,T splitItem)
{
// First we get all the indexes where the string is found,
// adding the last index of the array
var indexes = s.Select((b, i) => b.Equals(splitItem) ? i : -1).Where(i => i != -1)
.Union(new int[] { s.Count() }).ToArray();
int skip = 0; //variable to know where the next chunk starts
foreach (int index in indexes)
{
IEnumerable<T> array = s.Skip(skip).Take(index - skip).ToArray();
yield return array; //we return the chunk
skip = index+1;
}
}
用法:
foreach(var splitted in stringArray.SplitArray("@"))
{
//splited is a string[]
}