将内容数量有限的字符串数组内容从最后一个数组位置传输到新的字符串数组
Transfer String Array Contents with limited number of contents to new String Array from last Array position
我有一个名为 "string_array_packet" 的字符串数组,其中包含
FA,6,52,4C,0,A4,FA,6,52,4C,0,A4,FA,6,52,4C,0,A4,FA,6,52,4C,0 ,A4,FA,6,52,4C,0,A4,FA,6,52,4C,0,A4,FA,6,52,4C,0,A4,FA,6,
现在我需要检查从 Array(string_array_packet) 末尾到另一个字符串数组的内容 "FA,6,52,4C,0,A4,"(这是正确的响应)是否存在。在上面的数组内容中,考虑到 FA 是每个响应的起始字节,正确的响应从 string_array_packet[Length-8] 开始。但是索引位置可能会有所不同,因为数组内容是动态生成的。我正在尝试以下操作,但我知道如果职位发生变化,它将无法正常工作。任何帮助将不胜感激
string_array_packet[string_array_packet.Length - 8]);
您可以执行以下操作。
var strArray = new string[]{"FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6"};
var searchArray = new string[]{"FA","6","52","4C","0","A4"};
var index = strArray.FindAllIndexOf("FA")
.Reverse()
.First(x=>strArray
.Skip(x)
.Take(searchArray.Count())
.SequenceEqual(searchArray));
var result = strArray.Skip(index).Take(searchArray.Count());
其中 FindAllIndexOf 定义为
public static class Extension
{
public static int[] FindAllIndexOf<T>(this IEnumerable<T> values, T val)
{
return values.Select((b,i) => object.Equals(b, val) ? i : -1)
.Where(i => i != -1)
.ToArray();
}
}
这将输出为
index : 36
result : FA,6,52,4C,0,A4
我有一个名为 "string_array_packet" 的字符串数组,其中包含
FA,6,52,4C,0,A4,FA,6,52,4C,0,A4,FA,6,52,4C,0,A4,FA,6,52,4C,0 ,A4,FA,6,52,4C,0,A4,FA,6,52,4C,0,A4,FA,6,52,4C,0,A4,FA,6,
现在我需要检查从 Array(string_array_packet) 末尾到另一个字符串数组的内容 "FA,6,52,4C,0,A4,"(这是正确的响应)是否存在。在上面的数组内容中,考虑到 FA 是每个响应的起始字节,正确的响应从 string_array_packet[Length-8] 开始。但是索引位置可能会有所不同,因为数组内容是动态生成的。我正在尝试以下操作,但我知道如果职位发生变化,它将无法正常工作。任何帮助将不胜感激
string_array_packet[string_array_packet.Length - 8]);
您可以执行以下操作。
var strArray = new string[]{"FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6","52","4C","0","A4","FA","6"};
var searchArray = new string[]{"FA","6","52","4C","0","A4"};
var index = strArray.FindAllIndexOf("FA")
.Reverse()
.First(x=>strArray
.Skip(x)
.Take(searchArray.Count())
.SequenceEqual(searchArray));
var result = strArray.Skip(index).Take(searchArray.Count());
其中 FindAllIndexOf 定义为
public static class Extension
{
public static int[] FindAllIndexOf<T>(this IEnumerable<T> values, T val)
{
return values.Select((b,i) => object.Equals(b, val) ? i : -1)
.Where(i => i != -1)
.ToArray();
}
}
这将输出为
index : 36
result : FA,6,52,4C,0,A4