Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
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



VB
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,
Posted
Comments
Andreas Gieriet 6-Oct-13 11:28am    
How to know where a record ends? Or what is the criterion to add a newline instead of a comma?
Andi
safiuddin nawaz 6-Oct-13 11:57am    
like im having a foreach loop to read from treeview after the loop is complete i have to store it in another row
Andreas Gieriet 6-Oct-13 16:14pm    
I don't understand this. You show here some text and in the same go you talk about a treeview, loop, rows. This does not make any sense to me. The text in your question has nothing in common with a treeview and I see no loop - you seem to confuse things...

Assuming you break before any field that does not start on a number, you may try the following:
C#
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();
}


Cheers
Andi
 
Share this answer
 
v2
Comments
ridoy 6-Oct-13 12:52pm    
good solution,my 5.
safiuddin nawaz 6-Oct-13 13:52pm    
how do i split a string which ends with a number
string abc=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,;


how do i split this string and how can i save them row by row

MANUFACTURE_FORM 1 1 1 1
CATEGORY_FORM 1 1 1 1
Andreas Gieriet 6-Oct-13 15:23pm    
I would say that I leave that for your homework... ;-)
Did you understand the code above?
The above algorithm is: get all fields of the original text and add a new-line before each non-empty-and-non-digit field, except the first one, and add a new-line at the end if there was at least one field.
Cheers
Andi
PS: Your original problem had "rows" without digits-fields, that's why I came up with this quick solution.
BillWoodruff 7-Oct-13 0:25am    
Excellent answer, upvoted.
Andreas Gieriet 7-Oct-13 1:37am    
Hello BillWoodruff,
Thanks for your 5!
Cheers
Andi
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:
SQL
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 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.
 
Share this answer
 
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,
 
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