Reputation: 23
I want to insert data with for loop by a split comma here comma values will be treat as a column value and semicolon will treat as a new row:
string st = "test1,test2;test3,test4;";
for(int i = 0; i < col.length; i++)
{
// insert into tableName (col,col) values(test1,test2);
}
// further it should loop like test3,test4...
Upvotes: 0
Views: 847
Reputation: 23797
You can get the data with Linq:
string st = "test1,test2;test3,test4;";
var data = st.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Split(','))
.Select(s => new { col1 = s[0], col2 = s[1]});
Upvotes: 0
Reputation:
You can use Regex.Split()
string input = "test1, test2; test3, test4";
var splitResult= Regex.Split(input, "; ");
foreach (data in splitResult)
var finalResul= Regex.Split(data, ",");
Upvotes: 0
Reputation: 30545
Check This:
foreach (var row in st.Split(";"))
{
var cols = row.Split(",");
var col1 = cols[0];
var col2 = cols[1];
/// your operation
}
Upvotes: 1
Reputation: 339
Use String.Split(''); to seperate out the way you want, and then add it to your database or what have you.
List<string> rows = "test1,test2;test3,test4;".Split(";");
for(var i = 0; i < rows.Count; i++)
{
List<string> columns = rows[i].Split(',');
for(var x = 0; x < columns.Count; x++)
{
database.add(columns[x]);
}
}
Without knowing WHAT you're adding it to, we can't help much more than this.
Upvotes: 0