Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Best way to create a tab delimited text file
Message
From
10/05/2010 11:58:42
 
 
To
10/05/2010 11:28:05
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01463952
Message ID:
01463965
Views:
53
>>I'm assembling data from SQL server and VFP sources into a tab delimited txt file with 13 fields in it.
>>My current approach is to step thru the tables, populate 13 properties for each record and then assemble the properties with a writeline statement, separating each field with quotes and a tab character.
>>It seems kind of primitive after COPY TO x DELIMITED.
>>Is there a better way?
>
>
>You could write a reusable method that outputs a string with a variable number of parameters
>
>
>	class Test
>	{
>		//______________________________________________________________________
>		static void Main()
>		{
>			Console.WriteLine(ToTabDelimited("field1", "field2"));
>
>			Console.WriteLine(ToTabDelimited(1223, 456));
>			Console.WriteLine(ToTabDelimited("road 23", "88000", "San Diego"));
>
>			Console.ReadLine();
>
>		}
>		//______________________________________________________________________
>		static string ToTabDelimited(params object[] list)
>		{
>			StringBuilder sb = new StringBuilder();
>
>			for (var i = -1; ++i < list.Length; )
>			{
>				sb.AppendFormat("\"{{{0}}}\"\t", i);
>			}
>						
>			return String.Format(sb.ToString(0, sb.Length - 1), list);
>		}
>	}
>


A bit better
		static string ToTabDelimited(params object[] list)
		{
			StringBuilder sb = new StringBuilder();

			for (var i = -1; ++i < list.Length; )
			{
				sb.AppendFormat("\"{0}\"\t", list[i]);
			}
	

			return sb.ToString(0, Math.Max(sb.Length - 1, 0));
		}
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform