Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
string str=2 3 40 300 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 4 30 200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 2 30 200

the above str has to display like this

2 3 40 300
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Posted
Comments
Chandrashekar SK 2-Aug-12 2:29am    
Is there any particular reason why you are splitting like this and will the 4 spaces consistent??
Sandeep Mewara 2-Aug-12 2:37am    
What did you try so far? Where are you stuck?
Sergey Alexandrovich Kryukov 2-Aug-12 2:43am    
This would be "garbage in -- garbage our" code. Why not doing it by yourself, if you need to produce some (perhaps working) trash. Why would anyone help?
--SA

OK - if you split it based on a space, you can easily write code that displays it and inserts a line break every fourth string.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Aug-12 2:41am    
Best answer so far -- a 5. :-)
--SA
Hi,
You missed one "0" before "3 2 30 20". I added that. Now the result is perfect..
Try this:
C#
private void GetFormat()
{
    string str = "2 3 40 300 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 4 30 200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 2 30 200";
    string[] s4 = str.Split(' ');
    int count = 0;
    foreach (string s2 in s4)
    {
        if (count < 4)
        {
            Console.Write(s2 + "\t");
            count++;
        }
        else
        {
            Console.WriteLine("");
            count = 0;
        }
    }
}


Let me know if it works for you.
--Amit
 
Share this answer
 
Comments
_Amy 2-Aug-12 2:50am    
Who fond the error? Where is the error in this code? Why downvoted?
Hi,
C#
int i=0;
 foreach( var Parts in str.Split(' '))
  {
    Console.Out.writeLine(Parts+" ");
    i++;
    if(i==4)
    {
    Console.Out.WriteLine("");
    i=0;
    }
  }
 
Share this answer
 
Comments
Suvabrata Roy 2-Aug-12 3:38am    
why devoted...? it will work... try it
Try this. I have assumed here you are using console application . If not then reply.
char[] chArray= str.ToCharArray();
for(int i =0; i< chArray.Length;)
{
Console.Writeline(chArray[i]+" " + chArray[i+1] + " " + chArray[i+1] + " " + chArray[i+1]);
i= i+4;
}
 
Share this answer
 
If you really are that interested there is a way of getting all you need in one go:

XML
(\d+\s\d+\s\d+\s\d+)*



VB
Private Function GetPages(ByVal input As String) As List(of String)
    Dim output as New List(of String)
    Dim Numbers As String = "(\d+\s\d+\s\d+\s\d+)*"

    For Each Match As Match In Regex.Matches(input, Numbers)
        If Not Match.Value = String.Empty Then
                    output.add(Match.Value)
        End If
    Next

    Return output
End Function
 
Share this answer
 
v2

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