我有以下字符串:如何解析這個逗號分隔的字符串?
"'2014' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'"
我想加盟位置1和2一起,將它保存到正確位置1,例如:
"'2014381' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'"
我有以下字符串:如何解析這個逗號分隔的字符串?
"'2014' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'"
我想加盟位置1和2一起,將它保存到正確位置1,例如:
"'2014381' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'"
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);
之前,您尋找的是簡單而智能的。謝謝!只有一個空間b/w兩個不應該在那裏,我應該能夠刪除它!再次感謝! – user1683987
使用replaceFirst功能在此指出的問題:
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);
}
你可以再做:
string updated = ReplaceFirst(original, "' , '", "");
使用String.Split
和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 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);
輸出:
'2014381' , '381' , '1' , 'Eastern 10' , 'Wes 10' , '1'
'Split()';)也許.. – bonCodigo
而且,在詢問 –