---
title: "Split string by # and store result in a datatable"  
description: "Split string by # and store result in a datatable"  
author: "Jayden Bell"  
published: 2014-11-14  
updated: 2014-11-14  
canonical: https://www.mindstick.com/forum/12584/split-string-by-and-store-result-in-a-datatable  
category: "asp.net"  
tags: ["c#", "string", "split"]  
reading_time: 2 minutes  

---

# Split string by # and store result in a datatable

I have this [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp):

1#3.doc#0.036/n

2#1.doc#0.026/n

I want to [split](https://www.mindstick.com/blog/11920/top-3-voltas-split-acs-for-your-home) it on # and [put](https://answers.mindstick.com/qa/111890/can-you-explain-the-difference-between-put-and-post-http-methods) every line in a [single row](https://www.mindstick.com/forum/12960/how-do-i-get-a-single-row-from-a-linq-expression-in-c-sharp) inside [DataTable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) like this:

1 3.doc 0.036

2 1.doc 0.026

I have a DataTable like this:

DataTable [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) = new DataTable();

table.Columns.Add("Id", typeof([int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql)));

table.Columns.Add("[FileName](https://www.mindstick.com/interview/23463/difference-between-include-filename-and-include-filename)", typeof(string));

table.Columns.Add("[Content](https://www.mindstick.com/articles/13019/get-the-best-content-marketing-pricing-packages-for-your-brand)", typeof(string));

How can I do that?

## Replies

### Reply by Anonymous User

Here is how you would split a string into lines, and then those lines into different parts.

Your string is first split by the new line character \n into an array of lines string[].

Then those lines, one by one, are split into parts by Split('#').

And finally those parts are added to your table with the columns you created.

Remember to save the columns you created and don't forget to add the newly created [row](https://www.mindstick.com/forum/1212/this-row-already-belongs-to-this-table) to the table.

```
DataTable table = new DataTable();DataColumn colID = table.Columns.Add("Id", typeof(int));DataColumn colFileName = table.Columns.Add("FileName", typeof(string));DataColumn colContent = table.Columns.Add("Content", typeof(string));string source = "1#3.doc#0.036\n2#1.doc#0.026\n";string[] lines = source.Split('\n');foreach(var line in lines){    string[] split = line.Split('#');    DataRow row = table.NewRow();    row.SetField(colID, int.Parse(split[0]));    row.SetField(colFileName, split[1]);    row.SetField(colContent, split[2]);    table.Rows.Add(row);}
```

Adding data to the row with row["FileName"] = data is also possible, but this will break if you change the name of your column, while references to the column objects are checked by the compiler and your IDE. Also this article explains how to create a **typed DataTable**, which is something you may want to do.


---

Original Source: https://www.mindstick.com/forum/12584/split-string-by-and-store-result-in-a-datatable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
