询问者
数组拆分问题

常规讨论
-
一个数组{"A","0","1","2","3","A","11","22","33","A","44","55","A","66"},按照数组中“A”开头进行拆分,拆分成这样几个新的数组,如下:
{"A","0","1","2","3"}
{"A","11","22","33"}
{"A","44","55"}
{"A","66"}我的思路是用循环查找出第一个“A”的索引和下一个“A”的索引,然后提取两个索引之间的数据放到一个新的数组中。不知道有没有更好更方便更高效的算法?
努力~
全部回复
-
Hi,
感谢你在MSDN论坛发帖。
我没能想到更好的办法,因为你需要拿到每个元素的值,我一开始想使用list里面的find函数,查找第一个元素,但是后面的元素就得不到了。
你这个问题,我建议你把帖子的类型改成讨论贴,这样会有大量的社区成员进来,参与讨论。
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
-
Hi,
感谢你在MSDN论坛发帖。
我没能想到更好的办法,因为你需要拿到每个元素的值,我一开始想使用list里面的find函数,查找第一个元素,但是后面的元素就得不到了。
你这个问题,我建议你把帖子的类型改成讨论贴,这样会有大量的社区成员进来,参与讨论。
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
努力~
-
string [] array={"A","0","1","2","3","A","11","22","33","A","44","55","A","66"};
List<string> group=new List<string>(); //记录每一组
foreach(string str in array)
{
if(str.Equals("A"))
{
group=new List<string>();
group.Add(str);
}
else
group.Add(str);}
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
-
string [] array={"A","0","1","2","3","A","11","22","33","A","44","55","A","66"};
List<string> group=new List<string>(); //记录每一组
foreach(string str in array)
{
if(str.Equals("A"))
{
group=new List<string>();
group.Add(str);
}
else
group.Add(str);}
专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
努力~
-
补充一点,如果你的回来参数是只有单纯的A,那这样的确是比较简单,但如果回来的字母是有变化的,那你大量的用If就会减低效能,这点在流程与逻辑需要注意。如果你要事先判断它是否为字母,那你可能这样写会比较适合:
public static bool IsNumeric(String pNumber) { Regex NumberPattern = new Regex("[^0-9.-]"); return !NumberPattern.IsMatch(pNumber); }
然后替代掉你要判断A这件事。当然程式就要做些改变,因为在程式里的基本概念,就是要尽可能减少使用判断式以降低效能,以上是一些頠外的建议。
- 已编辑 電腦神手吳子陵 2017年9月7日 4:20