-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
37 lines (33 loc) · 1.05 KB
/
StringExtensions.cs
File metadata and controls
37 lines (33 loc) · 1.05 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
namespace Waher.Runtime.Text
{
/// <summary>
/// String extensions
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Gets a row from a text document, represented as a string.
/// </summary>
/// <param name="Text">Text document.</param>
/// <param name="LineNumber">Line number (1=first row).</param>
/// <returns>Row, or empty string if line number outside of available rows.</returns>
public static string GetRow(this string Text, int LineNumber)
{
if (LineNumber <= 0)
return string.Empty;
Text = Text.Replace("\r\n", "\n").Replace('\r', '\n');
int i = Text.IndexOf('\n');
int j = 0;
while (LineNumber > 1 && i >= 0)
{
j = i + 1;
i = Text.IndexOf('\n', j);
LineNumber++;
}
if (i < j)
return string.Empty;
else
return Text.Substring(j, i - j);
}
}
}