Reputation: 3
I need to split the text file (tab delimited) in columns and load in SQL Server. The problem is data in the file is not in a same manner there are spaces in columns like this:
Name(Single Space)Sex(Multiple Spaces)Designation(Single Space)Salary(Multiple Spaces)
I am using following code to split the files but its not according to my need.
for (int index = 2; index < allLines.Length; index++)
{
// split up the data line into its parts, using "|" as separator
//string[] items = allLines[index].Split(new char[] { '\t' });
string[] items = allLines[index].Split(new[] { '\t', '\n' });
cmd.Parameters["@Name"].Value = items[0];
cmd.Parameters["@GENDER"].Value = items[1];
cmd.Parameters["@CITY"].Value = items[2];
cmd.ExecuteNonQuery();
}
Upvotes: 0
Views: 795
Reputation: 6744
If the .Split
is working fine but the values you are adding into the query have extra whitespace you can use the String.Trim method to remove any extra white space. I would use it as follows:
cmd.Parameters["@Name"].Value = items[0].Trim();
cmd.Parameters["@GENDER"].Value = items[1].Trim();
cmd.Parameters["@CITY"].Value = items[2].Trim();
Upvotes: 1
Reputation: 8318
You can use regex to match words(consecutive sequences of characters), so that you can avoid splitting at all.
Try the following
var items = Regex.Matches(allLines,@"\w+").OfType<Match>().Select(mt => mt.Value);
Upvotes: 0
Reputation: 3190
You could use a Regex replace in order to dedupe double tabs in the data
string data = Regex.Replace(input, "\\t+", "\t");
The \t+
will match any occurrence of one or more tabs and replace it with a single tab.
Upvotes: 0
Reputation: 12196
There is an overloading method for Split that handle those empty entries.
string[] items = allLines[index].Split(new[] { '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries);
Upvotes: 1