This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DelimitedStringValues.cs
275 lines (254 loc) · 10.2 KB
/
DelimitedStringValues.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace RCNet.CsvTools
{
/// <summary>
/// Implements the single row of the delimited string values (csv format).
/// </summary>
public class DelimitedStringValues
{
//Constants
//Delimiters
/// <summary>
/// The semicolon delimiter.
/// </summary>
public const char SemicolonDelimiter = ';';
/// <summary>
/// The comma delimiter.
/// </summary>
public const char CommaDelimiter = ',';
/// <summary>
/// The tabelator delimiter.
/// </summary>
public const char TabDelimiter = '\t';
/// <summary>
/// The default delimiter.
/// </summary>
public const char DefaultDelimiter = SemicolonDelimiter;
//Attribute properties
/// <summary>
/// The current delimiter.
/// </summary>
public char Delimiter { get; private set; }
/// <summary>
/// The collection of the string values.
/// </summary>
public List<string> StringValueCollection { get; }
//Constructor
/// <summary>
/// Creates an empty instance.
/// </summary>
/// <param name="delimiter">The delimiter of the string values.</param>
public DelimitedStringValues(char delimiter = DefaultDelimiter)
{
Delimiter = delimiter;
StringValueCollection = new List<string>();
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="data">The string consisting of the delimited values.</param>
/// <param name="delimiter">The delimiter of the string values.</param>
public DelimitedStringValues(string data, char delimiter)
{
Delimiter = delimiter;
StringValueCollection = new List<string>();
LoadFromString(data, false, false);
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="data">The string consisting of the delimited values.</param>
public DelimitedStringValues(string data)
{
StringValueCollection = new List<string>();
LoadFromString(data, false, true);
return;
}
//Properties
/// <summary>
/// Number of stored string values.
/// </summary>
public int NumOfStringValues { get { return StringValueCollection.Count; } }
//Methods
//Static methods
/// <summary>
/// Tries to recognize a delimiter used in the sample data.
/// </summary>
/// <param name="sampleDelimitedData">The sample data row.</param>
/// <returns>The recognized delimiter or the default delimiter.</returns>
public static char RecognizeDelimiter(string sampleDelimitedData)
{
//Check of the presence of candidate chars
//Is "tab" char the candidate?
if (sampleDelimitedData.IndexOf(TabDelimiter) != -1)
{
//If tab is present then it is the most probable delimiter
return TabDelimiter;
}
//Is "semicolon" char the candidate?
if (sampleDelimitedData.IndexOf(SemicolonDelimiter) != -1)
{
//If semicolon is present then it is the next most probable delimiter
return SemicolonDelimiter;
}
//Recognize a floating point char
char floatingPointChar = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
if (sampleDelimitedData.IndexOf('.') != -1)
{
int index = sampleDelimitedData.IndexOf('.');
if (index > 0 && index < sampleDelimitedData.Length - 1)
{
char charBefore = sampleDelimitedData[index - 1];
if (charBefore >= '0' && charBefore <= '9')
{
char charAfter = sampleDelimitedData[index + 1];
if (charAfter >= '0' && charAfter <= '9')
{
floatingPointChar = '.';
}
}
}
}
//Is "comma" char the candidate?
if (sampleDelimitedData.IndexOf(CommaDelimiter) != -1 && floatingPointChar != CommaDelimiter)
{
//Comma is the probable delimiter
return CommaDelimiter;
}
else
{
//Remaining default delimiter
return DefaultDelimiter;
}
}
/// <summary>
/// Builds the single string consisting of the string values delimited by specified delimiter.
/// </summary>
/// <param name="stringValueCollection">The collection of alone string values.</param>
/// <param name="delimiter">The delimiter to be used.</param>
/// <returns>The built single string.</returns>
public static string ToString(IEnumerable<string> stringValueCollection, char delimiter = DefaultDelimiter)
{
StringBuilder output = new StringBuilder();
bool firstVal = true;
foreach (string value in stringValueCollection)
{
if (!firstVal)
{
output.Append(delimiter);
}
output.Append(value);
firstVal = false;
}
return output.ToString();
}
/// <summary>
/// Splits the string consisting of the delimited values.
/// </summary>
/// <param name="stringRow">The string consisting of the delimited values.</param>
/// <param name="delimiter">The used delimiter of the values.</param>
/// <returns>List of alone string values.</returns>
public static List<string> ToList(string stringRow, char delimiter = DefaultDelimiter)
{
List<string> values = new List<string>();
if (stringRow.Length > 0)
{
char[] allowedDelims = new char[1];
allowedDelims[0] = delimiter;
values.AddRange(stringRow.Split(allowedDelims, StringSplitOptions.None));
}
return values;
}
//Instance methods
/// <summary>
/// Clears the internal collection of the string values.
/// </summary>
public void Reset()
{
StringValueCollection.Clear();
return;
}
/// <summary>
/// Changes the delimiter.
/// </summary>
/// <param name="delimiter">The new delimiter.</param>
public void ChangeDelimiter(char delimiter)
{
Delimiter = delimiter;
return;
}
/// <summary>
/// Adds the next string value into the internal collection of string values.
/// </summary>
/// <param name="value">The value to be added.</param>
/// <returns>The number of string values within the internal collection after the operation.</returns>
public int AddValue(string value)
{
StringValueCollection.Add(value);
return StringValueCollection.Count;
}
/// <summary>
/// Removes a string value at the specified position from the internal collection.
/// </summary>
/// <param name="index">The zero-based index of a string value to be removed.</param>
/// <returns>The number of string values within the internal collection after the operation.</returns>
public int RemoveAt(int index)
{
StringValueCollection.RemoveAt(index);
return StringValueCollection.Count;
}
/// <summary>
/// Removes the trailing empty or white spaced string values from the inner collection.
/// </summary>
/// <returns>The number of string values within the internal collection after the operation.</returns>
public int RemoveTrailingWhites()
{
while (StringValueCollection.Count > 0 && StringValueCollection[StringValueCollection.Count - 1].Trim() == string.Empty)
{
StringValueCollection.RemoveAt(StringValueCollection.Count - 1);
}
return StringValueCollection.Count;
}
/// <summary>
/// Loads the delimited string values into the inner collection.
/// </summary>
/// <param name="stringRow">The string consisting of the delimited values.</param>
/// <param name="reset">Specifies whether to clear the internal collection before the load.</param>
/// <param name="recognizeDelimiter">When false then instance delimiter to be used. When true then method tries to recognize a proper delimiter from the data.</param>
/// <returns>The number of string values within the internal collection after the operation.</returns>
public int LoadFromString(string stringRow, bool reset = true, bool recognizeDelimiter = false)
{
if (reset)
{
Reset();
}
Delimiter = recognizeDelimiter ? RecognizeDelimiter(stringRow) : Delimiter;
StringValueCollection.AddRange(ToList(stringRow, Delimiter));
return StringValueCollection.Count;
}
/// <summary>
/// Gets the string value from the internal collection at the specified zero-based index.
/// </summary>
/// <param name="idx">The zero-based index of the value.</param>
/// <returns>A string value from the internal collection.</returns>
public string GetValueAt(int idx)
{
return StringValueCollection[idx];
}
///<inheritdoc cref="List{T}.IndexOf(T)"/>
public int IndexOf(string item)
{
return StringValueCollection.IndexOf(item);
}
///<inheritdoc/>
public override string ToString()
{
return ToString(StringValueCollection, Delimiter);
}
}//DelimitedStringValues
}//Namespace