-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrCounter.cs
201 lines (187 loc) · 7.5 KB
/
StrCounter.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
namespace StringCounter
{
/// <summary>
/// A class that can be used to count the number of words in a string,<br/>
/// or get the first letter of each word in a string.
/// </summary>
public static class StrCounter
{
/// <summary>
/// Counts the number of words in the current string.
/// </summary>
/// <param name="String">The <see langword="string"/> to inspect.</param>
/// <returns>The number of words in the current string.</returns>
public static int CountWords(this string String)
{
if ((String.Contains("a") == true || String.Contains("A") == true) == true ||
(String.Contains("b") == true || String.Contains("B") == true) == true ||
(String.Contains("c") == true || String.Contains("C") == true) == true ||
(String.Contains("d") == true || String.Contains("D") == true) == true ||
(String.Contains("e") == true || String.Contains("E") == true) == true ||
(String.Contains("f") == true || String.Contains("F") == true) == true ||
(String.Contains("g") == true || String.Contains("G") == true) == true ||
(String.Contains("h") == true || String.Contains("H") == true) == true ||
(String.Contains("i") == true || String.Contains("I") == true) == true ||
(String.Contains("j") == true || String.Contains("J") == true) == true ||
(String.Contains("k") == true || String.Contains("K") == true) == true ||
(String.Contains("l") == true || String.Contains("L") == true) == true ||
(String.Contains("m") == true || String.Contains("M") == true) == true ||
(String.Contains("n") == true || String.Contains("N") == true) == true ||
(String.Contains("o") == true || String.Contains("O") == true) == true ||
(String.Contains("p") == true || String.Contains("P") == true) == true ||
(String.Contains("q") == true || String.Contains("Q") == true) == true ||
(String.Contains("r") == true || String.Contains("R") == true) == true ||
(String.Contains("s") == true || String.Contains("S") == true) == true ||
(String.Contains("t") == true || String.Contains("T") == true) == true ||
(String.Contains("u") == true || String.Contains("U") == true) == true ||
(String.Contains("v") == true || String.Contains("V") == true) == true ||
(String.Contains("w") == true || String.Contains("W") == true) == true ||
(String.Contains("x") == true || String.Contains("X") == true) == true ||
(String.Contains("y") == true || String.Contains("Y") == true) == true ||
(String.Contains("z") == true || String.Contains("Z") == true) == true)
{
bool isWord = false,
wordStarted = false;
int wordCount = 0;
for (int i = 0; i < String.Length; i++)
{
char char_ = String[i];
char upperComma = '\'';
bool isLetter = char.IsLetter(char_);
bool isUpperComma = String[i] == upperComma;
if (isLetter == true)
{
isWord = true;
if (i > 0)
{
if (char.IsLetter(String[i - 1]) == false ||
char.IsUpper(char_) == true && char.IsLetter(String[i - 1]) == false)
wordStarted = true;
}
else if (i == 0)
{
if (char.IsLetter(String[i]) == true &&
char.IsUpper(char_) == true || char.IsLower(char_))
wordStarted = true;
}
}
else if (isLetter == false)
{
isWord = false;
wordStarted = false;
}
if (isWord == true)
{
if (wordStarted == true)
{
wordCount++;
wordStarted = false;
}
}
if (i > 0) if (isUpperComma == true && char.IsLetter(String[i - 1])) wordCount--;
}
return wordCount;
}
else return 0;
}
/// <summary>
/// Gets the first letter of each word in the current string.
/// </summary>
/// <param name="String">The <see langword="string"/> to inspect.</param>
/// <returns>An array of characters (a <see langword="string"/>) that contains the first letter of each word in the string.</returns>
public static string GetWordBeginnings(this string String)
{
if ((String.Contains("a") == true || String.Contains("A") == true) == true ||
(String.Contains("b") == true || String.Contains("B") == true) == true ||
(String.Contains("c") == true || String.Contains("C") == true) == true ||
(String.Contains("d") == true || String.Contains("D") == true) == true ||
(String.Contains("e") == true || String.Contains("E") == true) == true ||
(String.Contains("f") == true || String.Contains("F") == true) == true ||
(String.Contains("g") == true || String.Contains("G") == true) == true ||
(String.Contains("h") == true || String.Contains("H") == true) == true ||
(String.Contains("i") == true || String.Contains("I") == true) == true ||
(String.Contains("j") == true || String.Contains("J") == true) == true ||
(String.Contains("k") == true || String.Contains("K") == true) == true ||
(String.Contains("l") == true || String.Contains("L") == true) == true ||
(String.Contains("m") == true || String.Contains("M") == true) == true ||
(String.Contains("n") == true || String.Contains("N") == true) == true ||
(String.Contains("o") == true || String.Contains("O") == true) == true ||
(String.Contains("p") == true || String.Contains("P") == true) == true ||
(String.Contains("q") == true || String.Contains("Q") == true) == true ||
(String.Contains("r") == true || String.Contains("R") == true) == true ||
(String.Contains("s") == true || String.Contains("S") == true) == true ||
(String.Contains("t") == true || String.Contains("T") == true) == true ||
(String.Contains("u") == true || String.Contains("U") == true) == true ||
(String.Contains("v") == true || String.Contains("V") == true) == true ||
(String.Contains("w") == true || String.Contains("W") == true) == true ||
(String.Contains("x") == true || String.Contains("X") == true) == true ||
(String.Contains("y") == true || String.Contains("Y") == true) == true ||
(String.Contains("z") == true || String.Contains("Z") == true) == true)
{
bool isWord = false,
wordStarted = false;
int wordCount = 0;
string wordBegins = string.Empty;
for (int i = 0; i < String.Length; i++)
{
char char_ = String[i];
char upperComma = '\'';
bool isLetter = char.IsLetter(char_);
bool isUpperComma = String[i] == upperComma;
if (isLetter == true)
{
isWord = true;
if (i > 0)
{
if (char.IsLetter(String[i - 1]) == false ||
(char.IsUpper(char_) == true && char.IsLetter(String[i - 1]) == false))
wordStarted = true;
}
else if (i == 0)
{
if (char.IsLetter(String[i]) == true &&
(char.IsUpper(char_) == true || char.IsLower(char_)))
wordStarted = true;
}
}
else if (isLetter == false)
{
isWord = false;
wordStarted = false;
}
if (isWord == true)
{
if (wordStarted == true)
{
wordCount++;
if (i - 1 > -1)
{
if (String[i - 1] == '\'')
{
if (i + 1 != String.Length)
{
bool nextIsLetter = char.IsLetter(String[i + 1]);
if (nextIsLetter == true)
{
wordBegins += ", " + String[i];
}
else if (nextIsLetter == false)
{
// do nothing
}
}
}
else wordBegins += ", " + char_;
}
else wordBegins += ", " + char_;
wordStarted = false;
}
}
if (i > 0) if (isUpperComma == true && char.IsLetter(String[i - 1])) wordCount--;
}
return wordBegins.Substring(2);
}
else return string.Empty;
}
}
}