i have a string like '"foo bar"," ","boooo"," baz"'
i want split this using regex into:
"foo bar"," "
"boooo","baz"
thanks:
edit
sorry i don't speak very well in English..
i have a string and i want to split into 2 tokens + remove the second ',' i want to parse this with regex.
i tested this but not work:
("[^"]+","[^"]+"),
a string like "a","b" most not break
Doesn't this work?
var splittedValues = yourString.Split(',');
You can use the .Net built in method String.Split()
String[] splitString = "foo,bar,meow".Split(',');
you should look at TextFieldParser Class to handle this
working example that give the input that you provided and give you the output requested
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var test = "\"foo bar\",\" \",\"boooo\",\" baz\"";
var result = new List<string>();
using (var data = new MemoryStream(Encoding.UTF8.GetBytes(test)))
using (var parser = new TextFieldParser(data))
{
parser.Delimiters = new string[] {","};
parser.TrimWhiteSpace = false;
var fields = parser.ReadFields();
int i = 0;
foreach (var field in fields)
{
if ((i % 2) == 0)
{
result.Add(string.Format("\"{0}\"", (string.IsNullOrWhiteSpace(field) ? field : field.Trim())));
}
else
{
result[result.Count - 1] += string.Format(",\"{0}\"", (string.IsNullOrWhiteSpace(field) ? field : field.Trim()));
}
i++;
}
}
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadKey(false);
}
}
}
i have a string like '"foo bar"," ","boooo"," baz"'
i want split this using regex into:
"foo bar"," "
"boooo","baz"
thanks:
edit
sorry i don't speak very well in English..
i have a string and i want to split into 2 tokens + remove the second ',' i want to parse this with regex.
i tested this but not work:
("[^"]+","[^"]+"),
a string like "a","b" most not break
Doesn't this work?
var splittedValues = yourString.Split(',');
You can use the .Net built in method String.Split()
String[] splitString = "foo,bar,meow".Split(',');
you should look at TextFieldParser Class to handle this
working example that give the input that you provided and give you the output requested
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var test = "\"foo bar\",\" \",\"boooo\",\" baz\"";
var result = new List<string>();
using (var data = new MemoryStream(Encoding.UTF8.GetBytes(test)))
using (var parser = new TextFieldParser(data))
{
parser.Delimiters = new string[] {","};
parser.TrimWhiteSpace = false;
var fields = parser.ReadFields();
int i = 0;
foreach (var field in fields)
{
if ((i % 2) == 0)
{
result.Add(string.Format("\"{0}\"", (string.IsNullOrWhiteSpace(field) ? field : field.Trim())));
}
else
{
result[result.Count - 1] += string.Format(",\"{0}\"", (string.IsNullOrWhiteSpace(field) ? field : field.Trim()));
}
i++;
}
}
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadKey(false);
}
}
}
0 commentaires:
Enregistrer un commentaire