ReadAsLines、SplitLines
This commit is contained in:
1 parent
29f2ee58ac
commit
fbc25509bc
2 files changed
+10
-4
No files matched your search
@@ -174,6 +174,12 @@ public static class StringExtensions {
|
|||||||
private static readonly Regex regexLineEndingAndMerge = new(@"(?:\r\n|[\n\r\f\u0085\u2028\u2029])+", RegexOptions.Compiled);
|
private static readonly Regex regexLineEndingAndMerge = new(@"(?:\r\n|[\n\r\f\u0085\u2028\u2029])+", RegexOptions.Compiled);
|
||||||
private static readonly Regex regexLineEnding = new(@"\r\n|[\n\r\f\u0085\u2028\u2029]", RegexOptions.Compiled);
|
private static readonly Regex regexLineEnding = new(@"\r\n|[\n\r\f\u0085\u2028\u2029]", RegexOptions.Compiled);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将字符串分割为多行。
|
||||||
|
/// </summary>
|
||||||
|
public static string[] SplitLines(this string input, bool skipEmptyLines = false)
|
||||||
|
=> input.ReplaceLineEndings("\n").Split(['\n'], skipEmptyLines ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 正则
|
#region 正则
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ public static class FileUtils {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 读取文件中的所有内容,并按行分割。
|
/// 读取文件中的所有内容,并按行分割。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string[] ReadAsLines(string filePath, Encoding? encoding = null)
|
public static string[] ReadAsLines(string filePath, bool skipEmptyLines = false, Encoding? encoding = null)
|
||||||
=> FileUtils.ReadAsString(filePath, encoding).ReplaceLineEndings("\n").Split('\n');
|
=> FileUtils.ReadAsString(filePath, encoding).SplitLines(skipEmptyLines);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -47,14 +47,14 @@ public static class FileUtils {
|
|||||||
/// 若文件已存在,则会覆盖原文件。
|
/// 若文件已存在,则会覆盖原文件。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Write(string filePath, string text, Encoding? encoding = null)
|
public static void Write(string filePath, string text, Encoding? encoding = null)
|
||||||
=> Write(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text));
|
=> FileUtils.Write(filePath, (encoding ?? new UTF8Encoding()).GetBytes(text));
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建文件,并将 <paramref name="content" /> 写入文件。
|
/// 创建文件,并将 <paramref name="content" /> 写入文件。
|
||||||
/// 若文件已存在,则会覆盖原文件。
|
/// 若文件已存在,则会覆盖原文件。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Write(string filePath, IEnumerable<byte> content)
|
public static void Write(string filePath, IEnumerable<byte> content)
|
||||||
=> Write(filePath, [.. content]);
|
=> FileUtils.Write(filePath, [.. content]);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建文件,并将 <paramref name="content" /> 写入文件。
|
/// 创建文件,并将 <paramref name="content" /> 写入文件。
|
||||||
|
|||||||
Reference in new issue
Block a user