Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a test string "1,2,3,4,5,6,7,8,9,10,11,12,13"
I want the output to be
123
456
789
101112
13

I am not sure of the syntax for slpit. Can someone help?


string strMyTestMessage = "1,2,3,4,5,6,7,8,9,10,11,12,13";                        
string[] splitted = strMyTestMessage.Split(separator: new[] { "," }, count: 13, options: StringSplitOptions.None);
                                                
string strCountTest;

for (int i = 0; i < splitted.Length; i++)
{                            
if (i / 3 == 1)
 strCountTest = splitted[i].ToString() + "\n";                            
else
 strCountTest = splitted[i].ToString();

onsole.WriteLine(strCountTest);
								
}
Posted
Comments
Mehdi Gholam 6-Oct-14 15:40pm    
...and what is the rule for such a "split", every 3 commas?
Member 10683143 6-Oct-14 15:42pm    
yeah. That is what I was trying the i /3. Every 3 commas split the new line.
Sergey Alexandrovich Kryukov 6-Oct-14 15:44pm    
Why having this string in first place? (If this just for testing, I would understand, but still think about it.)
One simple advice: use the debugger (after to you answer the Mehdi's question). The problem is way too simple.
—SA
Member 10683143 6-Oct-14 15:45pm    
This was for testing. The real string is being built from database values.
Sergey Alexandrovich Kryukov 6-Oct-14 15:48pm    
That would mean wrong database schema design. When you need numeric data, use numeric types, when you need list data, use relations. Use normal forms and other good design stuff.
—SA

The split options look correct. However, int order to obtain the expected output you should modify your loop this way:
C#
for (int i = 0; i < splitted.Length; i++)
{
  if (i % 3 == 2)
    strCountTest = splitted[i].ToString() + "\n";
  else
    strCountTest = splitted[i].ToString();

  Console.Write(strCountTest);

}
 
Share this answer
 
v2
try this

C#
string strMyTestMessage = "1,2,3,4,5,6,7,8,9,10,11,12,13";
string[] strArray = strMyTestMessage.Split(',');
int iterations = strArray.Length;
for (int i = 0; i <= iterations; )
{
   Console.WriteLine(strArray[i] + ((i + 1) < iterations ? strArray[i + 1] : "") + ((i + 2) < iterations ? strArray[i + 2] : ""));

   i = i + 3;
}


good luck ;-)
 
Share this answer
 
I accepted CPallini322 solution because it was the if (i % 3 == 2) that lead me to my own solution using string builder. Sorry for the delayed response I was on vacation.

string strMyTestMessage = "1,2,3,4,5,6,7,8,9,10,11,12,13";
string[] splitted = strMyTestMessage.Split(separator: new[] { "," }, count: 13, options: StringSplitOptions.None);


            StringBuilder sb = new StringBuilder();
            string strOutput;

            for (int i = 0; i < 13; i++)
            {
                               
                strOutput = splitted[i].ToString();

                if (i % 3 == 2)
                {
                    sb.Append(strOutput).Append("\n");
                    strOutput = "";
                }
                else
                {
                    sb.Append(strOutput);
                    strOutput = "";
                }
                                           

            }
            Console.WriteLine(sb.ToString());
 
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