using System; using System.Collections.Generic; using System.Linq; namespace New_College.Common.Helper { public class StringHelper { /// /// 根据分隔符返回前n条数据 /// /// 数据内容 /// 分隔符 /// 前n条 /// 是否倒序(默认false) /// public static List GetTopDataBySeparator(string content, string separator, int top, bool isDesc = false) { if (string.IsNullOrEmpty(content)) { return new List() { }; } if (string.IsNullOrEmpty(separator)) { throw new ArgumentException("message", nameof(separator)); } var dataArray = content.Split(separator).Where(d => !string.IsNullOrEmpty(d)).ToArray(); if (isDesc) { Array.Reverse(dataArray); } if (top > 0) { dataArray = dataArray.Take(top).ToArray(); } return dataArray.ToList(); } } }