Click here to Skip to main content
15,916,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My input is :
C#
string[] sinput= { "1||age--11||gender--Female|||||", "0||age--16||gender--Male|||||" };


I need output in loop such as

C#
string soutput = ("age--11,age--16" );
string soutput = ("gender--Female,gender--Male" );


But I have tried myself, I couldn't achieve this solution.



private void button8_Click(object sender, EventArgs e)
       {
           string[] ss = { "1||age--11||gender--Female|||||", "0||age--16||gender--Male|||||" };

           for (int i = 0; i < ss.Length; i++)
           {
               string sss = ss[i].ToString().Replace("||",",").Replace(",,","");
           }

       }


What I have tried:

private void button8_Click(object sender, EventArgs e)
       {
           string[] ss = { "1||age--11||gender--Female|||||", "0||age--16||gender--Male|||||" };

           for (int i = 0; i < ss.Length; i++)
           {
               string sss = ss[i].ToString().Replace("||",",").Replace(",,","");
           }

       }
Posted
Updated 27-Apr-17 2:59am
v2

Split by comma, then by vertical bar. Search each array for strings beginning with the keywords "age" and "gender" and combine as necessary. BTW you do not need to call ToString on strings - it serves no purpose.
 
Share this answer
 
Comments
Maciej Los 27-Apr-17 14:38pm    
5ed!
You can try something like below here op1 and op2 strings will hold your output and have it updated as per your need.

string sss;
string[] ss = { "1||age--11||gender--Female|||||", "0||age--16||gender--Male|||||" };

string op1 = String.Empty;
string op2 = String.Empty;

for (int i = 0; i < ss.Length; i++)
{
     sss = ss[i].ToString().Replace("||",",").Replace(",,","");
     string[] p = sss.Split(',');
     op1 += String.Format("{0},", p[1]);//age details
     op2 += String.Format("{0},", p[2]);//gentder details
}
 
Share this answer
 
v3
Comments
Richard MacCutchan 27-Apr-17 9:18am    
Why are you calling ToString in something that is already a string?
[no name] 27-Apr-17 9:43am    
Yes, it's really not necessary. Updated solution.
Richard MacCutchan 28-Apr-17 3:42am    
One still there. And, for the same reason, why do you need string.Format?
[no name] 28-Apr-17 7:45am    
String.Format is there as you need to append ',' as seperator
Richard MacCutchan 28-Apr-17 9:01am    
My bad, missed that.
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
C#
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
C#
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:
C#
foreach (var s in s1.Where(s => s.StartsWith("age")))
{
    sb1.Append(s);
    if (i < ss.Length - 1) sb1.Append(",");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900