forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStringHelper.cs
More file actions
43 lines (38 loc) · 1.24 KB
/
StringHelper.cs
File metadata and controls
43 lines (38 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using System.Collections.Generic;
using System.Linq;
namespace Blog.Core.Common.Helper
{
public class StringHelper
{
/// <summary>
/// 根据分隔符返回前n条数据
/// </summary>
/// <param name="content">数据内容</param>
/// <param name="separator">分隔符</param>
/// <param name="top">前n条</param>
/// <param name="isDesc">是否倒序(默认false)</param>
/// <returns></returns>
public static List<string> GetTopDataBySeparator(string content, string separator, int top, bool isDesc = false)
{
if (string.IsNullOrEmpty(content))
{
return new List<string>() { };
}
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();
}
}
}