Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.. I have a little problem. How can I split the following string.

I would like to divide characters 32.(","). Note: String can be great more.

Always will be divided by 32.(",") characters. Thanks Help.


C#
string = 20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,


I want :

C#
20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,
20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,
20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,
Posted

Something along the line of:

C#
string z = @"20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,";
      System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"(\d{8},\d{4}-\d{2}-\d{2})");
      string[] y = reg.Split(z);


Please bear in mind that i'm not a Regex Guru at all... ;)

hope some of its helps
 
Share this answer
 
Try below code:
C#
string temp1 = @"20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,";

string[] temp = temp1.Split(',');
int total = temp.Length;
int totalIndex = temp.Length;

if (total % 32 == 0)
{
	totalIndex = total / 32;
}
else
{
	totalIndex = (total / 32) +1;
}
string[] tempResult = new string[totalIndex];

for (int idx = 0; idx < totalIndex; idx++)
{
	var tempArr = temp.Select((s, i) => new { i, s })
		   .Where(t => (t.i >= (idx * 32)) && (t.i <= ((idx +1)* 32 - 1)))
		   .Select(t => t.s)
			.ToArray();
	tempResult[idx] = string.Join(",", tempArr);
}

foreach (string t in tempResult)
{
	Console.WriteLine(t.ToString());
}
 
Share this answer
 
C#
string testString =
              "20151003,2015-10-03 18:17:36,83556593,1555907159,001,abcde,1,ggg,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,2555907157,002,sadfgfhg,1,fdghh,,,,,,,,,,,,,,,,,,,,,,,,,20151003,2015-10-03 18:17:36,83556593,3555907157,003,wertre,1,ewr,,,,,,,,,,,,,,,,,,,,,,,,,";
          var lines = new List<string>();
          int n = 0;
          int startIndex = 0;
          for (int i = 0; i < testString.Length; i++)
          {
              if (testString[i] == ',')
              {
                  n++;
                  if (n % 32 == 0)
                  {
                      lines.Add(testString.Substring(startIndex, i+1 - startIndex));
                      startIndex = i+1;
                  }
               }
           }
 
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