0

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'"
Dave Zych
  • 21,581
  • 7
  • 51
  • 66
user1683987
  • 533
  • 3
  • 7
  • 17

4 Answers4

3

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);

Demo

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3
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);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 1
    simple and smart. Thanks! there's only a space b/w the two which shouldn't be there, I should be able to remove that! Thanks Again! – user1683987 Jan 03 '13 at 17:03
0

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, "' , '", "");
Community
  • 1
  • 1
Paddy
  • 33,309
  • 15
  • 79
  • 114
0
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'
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179