-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrapeBit.cs
94 lines (75 loc) · 3.16 KB
/
ScrapeBit.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Linq;
public static class ScrapeBit
{
public static string FirstString(string SourceText, string ToFindText, string ReadUntilString)
{
int dIndex = SourceText.IndexOf(ToFindText); // Get Index in string of the find Text.
if (dIndex == -1) return null; // If Find Text is not found return.
SourceText = SourceText.Substring(dIndex + ToFindText.Length);
int FoundTerminators = 0;
IList<char> ReadTextChars = new List<char>(); //Where to store the good chars.
char[] SourceTextChar = SourceText.ToArray(); // To Char Array
char[] TerminatorChars = ReadUntilString.ToArray(); //To Char Array
for (int i = 0; i < SourceTextChar.Length; i++)
{
if (FoundTerminators == TerminatorChars.Length) break;
if (SourceText[i] == TerminatorChars[FoundTerminators])
{
FoundTerminators++;
continue;
}
FoundTerminators = 0;
ReadTextChars.Add(SourceText[i]); // Add the good char
}
if (ReadTextChars.Count <= 0) return null;
else return new string(ReadTextChars.ToArray());
}
public static string[] AllString(string SourceText, string ToFindText, string ReadUntilString)
{
IList<int> indexes = SourceText.AllIndexesOf(ToFindText);
IList<string> Results = new List<string>();
foreach(var index in indexes)
{
Results.Add(StringFromIndex(SourceText, ToFindText, ReadUntilString, index));
}
return Results.ToArray();
}
private static string StringFromIndex(string SourceText, string ToFindText, string ReadUntilString, int CustomStartIndex)
{
int dIndex = CustomStartIndex; // Get Index in string of the find Text.
if (dIndex == -1) return null;
SourceText = SourceText.Substring(dIndex + ToFindText.Length);
int FoundTerminators = 0;
IList<char> ReadTextChars = new List<char>(); //Where to store the good chars.
char[] SourceTextChar = SourceText.ToArray(); // To Char Array
char[] TerminatorChars = ReadUntilString.ToArray(); //To Char Array
for (int i = 0; i < SourceTextChar.Length; i++)
{
if (FoundTerminators == TerminatorChars.Length) break;
if (SourceText[i] == TerminatorChars[FoundTerminators])
{
FoundTerminators++;
continue;
}
FoundTerminators = 0;
ReadTextChars.Add(SourceText[i]); // Add the good char
}
if (ReadTextChars.Count <= 0) return null;
else return new string(ReadTextChars.ToArray());
}
private static List<int> AllIndexesOf(this string str, string value)
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException("the string to find may not be empty", "value");
List<int> indexes = new List<int>();
for (int index = 0; ; index += value.Length)
{
index = str.IndexOf(value, index);
if (index == -1)
return indexes;
indexes.Add(index);
}
}
}