I can't see any reason why you are using Replace on commas as there are no commas in your input string - the comma that appears is the array separator. The assignment to
string[] ss
is essentially the same as
ss[0] = "1||age--11||gender--Female|||||";
ss[1] = "0||age--16||gender--Male|||||" ;
You can
Split
each entry on the array on the vertical bar
|
. If you are sure that every item in the array will be in that same format then you could use the
StringSplitOptions.RemoveEmptyEntries
option.
If you are going to reconstruct a string from these items then you should use the
StringBuilder[
^] class
The code below assumes that each of the items in the array
ss
is in exactly the same format as shown in your question
string[] ss = { "1||age--11||gender--Female|||||", "0||age--16||gender--Male|||||" };
StringBuilder sb1 = new StringBuilder("(");
StringBuilder sb2 = new StringBuilder("(");
for (int i = 0; i < ss.Length; i++)
{
var s1 = ss[i].Split(new []{'|'}, StringSplitOptions.RemoveEmptyEntries);
sb1.Append(s1[1]);
if(i < ss.Length - 1) sb1.Append(",");
sb2.Append(s1[2]);
if (i < ss.Length - 1) sb2.Append(",");
}
sb1.Append(")");
sb2.Append(")");
Debug.Print(sb1.ToString());
Debug.Print(sb2.ToString());
Alternatively you can search for the item that begins with "age", "gender" - example:
foreach (var s in s1.Where(s => s.StartsWith("age")))
{
sb1.Append(s);
if (i < ss.Length - 1) sb1.Append(",");
}