Break up or Split a string with a keyword

Created by
Monday, June 13, 2016

 

Splitting strings in C# is failry straight forward. I found it a little trickier though when splitting up a massive string via a keyword rather than a characther.

 

public ActionResult DownLoadFile(string file)
{
	string fileContent = file;

	string[] fileItems = BuildCSV(fileContent);

	StringBuilder sb = new StringBuilder();
	
	foreach (var item in fileItems)
	{
		sb.AppendLine(item);
	}

	Response.Clear();
	Response.ClearHeaders();

	Response.AddHeader("Content-Length", file.Length.ToString());
	Response.ContentType = "text/plain";
	Response.AppendHeader("content-disposition", "attachment;filename=\"SOMETHING_" + file.Replace(".", "") + ".csv\"");

	Response.Write(sb);
	Response.End();

	return View("Index");
}

public string[] BuildCSV(string fileContent)
{
	var end = new string[] { "ENDS" };

	string[] fileItems = fileContent.Split(end, System.StringSplitOptions.None);

	return fileItems;
}
Tags:
c#
strings
0
Rating
1 2 3 4 5

Save

Saved

Report

Reported
Reasons
Cancel
0 comments