I have the following string:
"'2014' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'"
I would like to join Position 1 and 2 together and save it back in position 1, for example:
"'2014381' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'"
I have the following string:
"'2014' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'"
I would like to join Position 1 and 2 together and save it back in position 1, for example:
"'2014381' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'"
Use String.Split
and the String.Join
:
string text = "'2014' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'";
string[] words = text.Split(new[] { "' , '" }, StringSplitOptions.None);
string result = string.Join("", words.Take(2)) +
string.Join("' , '", words);
var str = "'2014' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'";
var parts = str.Split(new string[] { " , " }, StringSplitOptions.None);
parts[0] = String.Format("'{0}{1}'", parts[0].Replace("'", ""),
parts[1].Replace("'", ""));
str = String.Join(" , ", parts);
Using the replaceFirst function noted in this question:
How do I replace the *first instance* of a string in .NET?
string ReplaceFirst(string text, string search, string replace)
{
int pos = text.IndexOf(search);
if (pos < 0)
{
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}
You could then do:
string updated = ReplaceFirst(original, "' , '", "");
var input = "'2014' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'";
var delimeter = " , ";
var splits = input.Split(',').Select (i => i.Trim());
var result = splits.Take(2).Aggregate ((x, y) => x + y).Replace("''", "")
+ delimeter + splits.Skip(1).Aggregate ((x, y) => x + delimeter + y);
Output:
'2014381' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'