如何逐行插入逗号分隔字符串 [英] How to Insert comma seperated string one by one in a row

查看:64
本文介绍了如何逐行插入逗号分隔字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MANUFACTURE_FORM,1,1,1,1,CATEGORY_FORM,1,1,1,1,BRAND_FORM,1,1,1,1,SEX_CODE_FORM,1,1,1,1,SPORTS_FORM,1,1,1,1,SPORTS_ITEM_FORM,1,1,1,1,STOCK_MASTER_MAINTANINACE,STOCK_CARD_FROM,1,1,0,0,SALES_PRICE_FORM,1,1,0,0,STOCK_CARD_INQUIRY_FORM,1,1,0,0,IMAGE_UPLOAD,1,1,0,0,SIZE_COLOR,1,1,0,0,MAIN_ORDER_L.C_PROCESSING, L.C_PROCESSING_FORM,1,0,0,1,SHIPMENT_DETAILS_FROM,1,0,0,1,SUB_ORDER_L.C_PROCESSING,
ADD_SUBORDER_FORM,1,1,0,1,SUB_L.C_PROCESS_FORM,1,1,0,1,







怎么做我一个接一个地保存这个逗号分隔的字符串










how do i save this comma seperated string in one by one row



MASTER_FILE_MAINTENANCE,
MANUFACTURE_FORM,1,1,1,1,
CATEGORY_FORM,1,1,1,1,
BRAND_FORM,1,1,1,1,
SEX_CODE_FORM,1,1,1,1,
SPORTS_FORM,1,1,1,1,
SPORTS_ITEM_FORM,1,1,1,1,
STOCK_MASTER_MAINTANINACE,
STOCK_CARD_FROM,1,1,0,0,
SALES_PRICE_FORM,1,1,0,0,
STOCK_CARD_INQUIRY_FORM,1,1,0,0,
IMAGE_UPLOAD,1,1,0,0,
SIZE_COLOR,1,1,0,0
,MAIN_ORDER_L.C_PROCESSING,
 L.C_PROCESSING_FORM,1,0,0,1,
SHIPMENT_DETAILS_FROM,1,0,0,1,
SUB_ORDER_L.C_PROCESSING,
ADD_SUBORDER_FORM,1,1,0,1,
SUB_L.C_PROCESS_FORM,1,1,0,1,

推荐答案

假设您在任何不以数字开头的字段之前中断,您可以尝试以下方法:

Assuming you break before any field that does not start on a number, you may try the following:
string text = "..."; // your text
int count = 0;
foreach (string field in text.Split(','))
{
    if (string.IsNullOrEmpty(field))
    {
        Console.Write(",");
    }
    else if (char.IsDigit(field[0]))
    {
        Console.Write(",{0}", field);
    }
    else
    {
        if (count > 0)
        {
             Console.WriteLine(",");
        }
        Console.Write(field);
    }
    count++;
}
if (count > 0)
{
    Console.WriteLine();
}





干杯

Andi



Cheers
Andi


Are你通过解析/迭代TreeView的节点集合来创建字符串?如果你是,有一个简单的方法来创建/写出字符串。



如果你要解析一个字符串提供给你:你的示例字符串几乎是常规,因为类别条目后面有4个元素,除了:



STOCK_MASTER_MAINTANINACE

MAIN_ORDER_L.C_PROCESSING

SUB_ORDER_L.C_PROCESSING



如果你知道这个字符串总是,你可以通过insetring来规范它(填充)这三个类别名称之后的四个条目:如果你这样做,解析将更容易,因为你可以循环使用String.Split返回的字符串[]知道每五个项目将是一个类别名称,然后会立即跟随四个与条目名称相同的行的条目。



看起来有多难看一种操作是:
Are you creating the string by parsing/iterating-over a TreeView's Nodes Collection ? If you are, there's a simple way to create/write out the string.

If you are parsing a string supplied to you: your example string is almost "regular" in that category entries have 4 elements after them, except for:

STOCK_MASTER_MAINTANINACE
MAIN_ORDER_L.C_PROCESSING
SUB_ORDER_L.C_PROCESSING

If you knew that the string was always in this form, you could regularize it by insetring (padding) four entries after those three category names: if you did that, parsing would be much easier, because you could loop through the string[] returned by using String.Split knowing every fifth item would be a category name, and that it would be immediately followed by four entries that were meant to go on the same "line" as the category name.

Look how ugly that kind of manipulation is:
y = y.Insert(y.IndexOf("STOCK_MASTER_MAINTANINACE") + "STOCK_MASTER_MAINTANINACE".Length, ",null, null, null, null");

y = y.Insert(y.IndexOf("MAIN_ORDER_L.C_PROCESSING") + "MAIN_ORDER_L.C_PROCESSING".Length, ",null, null, null, null");

y = y.Insert(y.IndexOf("SUB_ORDER_L.C_PROCESSING") + "SUB_ORDER_L.C_PROCESSING".Length, ",null, null, null, null");

It将更多更好地让字符串的创建者/供应商填充没有条目的类别名称。



To您可以依赖对具有可预测格式的数据的程度,更简单的可以是您的代码来解析它,迭代它。



当然,如果你正在解析具有不同格式的东西,比如TreeView的节点集合,那么你必须编写一种不同类型的迭代器/解析器(这不一定很难)。



Andreas Gieriet的上述解决方案完全适用于您正在处理stri的情况ng以您在此处显示的格式提供给您:只要类别名称不以数字开头,并且类别名称后面的数据不以非数字字符开头,它将始终有效。

It would be so much better to get the creator/supplier of the string to "pad" the category names that have no entries.

To the extent you can rely on your data having a predictable format, the simpler can be your code to parse it, iterate over it.

Of course, if you are parsing something with a varying format, like the Nodes Collection of a TreeView, then you have to write a different kind of iterator/parser (which is not necessarily that difficult).

Andreas Gieriet's solution above is perfectly valid for the case that you are dealing with a string supplied to you in the format you've shown here: it will always work as long as a category name does not start with a number, and no "data" that follows the category name starts with a non-numeric character.


string str =x,y,y,z,v,k,l,p,o,kl =;

string [] strArray = str.Split(',');

int m = 0,t = 0;

string [] result = new string [strArray.Length];

for(int k = 0; k< strArray.Length; k ++)

{

if(kl ==)

{

kl = strArray [k] +,;

}

其他

{

kl = kl + strArray [k] +,;

}

if(m == 4)

{

result [t] = kl;

kl =;

t ++;

m = 0;

}

else if(k == strArray.Length - 1)

{

结果[t] = kl;

kl =;

m = 0;

}

其他

{

m = m + 1;

}

}

string hj =;

string hj =& quot;& quot ;;

foreach(结果中的字符串g)

{

console.writeline(" \ n" + g);

}< / pre>











Reult就像这样

x,y,y,z,v,

k,l,p,o,
string str = "x,y,y,z,v,k,l,p,o", kl="";
string[] strArray = str.Split(',');
int m = 0,t=0;
string[] result = new string[strArray.Length];
for (int k = 0; k < strArray.Length; k++)
{
if (kl == "")
{
kl = strArray[k]+",";
}
else
{
kl = kl + strArray[k] + ",";
}
if (m == 4)
{
result[t] = kl;
kl = "";
t++;
m = 0;
}
else if (k == strArray.Length - 1)
{
result[t] = kl;
kl = "";
m = 0;
}
else
{
m = m + 1;
}
}
string hj = "";
string hj = &quot;&quot;;
foreach (string g in result)
{
console.writeline("\n"+g);
}</pre>





Reult is like this
x,y,y,z,v,
k,l,p,o,


这篇关于如何逐行插入逗号分隔字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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