拆分字符串数组并作为字符串返回 [英] To split string array and return as a string

查看:132
本文介绍了拆分字符串数组并作为字符串返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入是:

My input is :

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



我需要循环输出,例如



I need output in loop such as

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(",,","");
           }

       }

推荐答案

以逗号分隔,然后以竖线分隔.在每个数组中搜索以关键字"age"和"gender"开头的字符串,并根据需要进行组合.顺便说一句,您不需要在字符串上调用ToString-它没有用.
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.


您可以在此处尝试如下操作op1和op2字符串将保存您的输出并根据您的需要.

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
}


我看不到在逗号上使用替换"的任何原因,因为输入字符串中没有逗号-出现的逗号是数组分隔符. string[] ss的分配与
基本相同
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|||||" ;

您可以在垂直条|Split数组上的每个条目.如果确定数组中的每个项目都采用相同的格式,则可以使用StringSplitOptions.RemoveEmptyEntries选项.

如果要从这些项目中重建字符串,则应使用 ^ ]类

下面的代码假定数组ss中的每个项目与问题中显示的格式完全相同.

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

或者,您可以搜索以"age","gender"开头的项目-例如:

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(",");
}


这篇关于拆分字符串数组并作为字符串返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆