I have a text file containing 10000s records. records are deliminated with comma ','. I read all the data into a string and i need to split the string at every 1000th comma.
Example:- If i had 10000 records then the splitted string array will have 10 records.
Is there any built in function to split at specific location by a character?

Thank You.

Recommended Answers

All 4 Replies

see the below example which can be help you. it's a just example just write your own code.

ex:- string str = "a,b,b,c,c,d,d,e,e,f,f,g,g,h,h,i,i,j,j,k,";
//now i want to split with the evry 2 comma
//so my o/p should be display like this
a,b
b,c
c,d
d,e
e,f
f,g
g,h
i,j
j,k
		for(int t=0;t<str.Length;t+=4)
		{
			if(t==str.Length){ break;}
			console.WriteLine((str.Substring(t,3) +"\n");
		}
commented: helpfull. +15

Here is a generic split method that takes a character and an integer (n) and splits your string on every nth occurance of the character. Included is a little Main routine that tests it.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Testing {
    class Program {
        static void Main(string[] args) {
            String test = "This, is, a, test, of, the, comma, splitting, thing, that, will, see, if, we, get, ten, or, not";

            string[] lines = SplitOnNth(test, ',', 3);

            foreach (string line in lines) {
                Console.WriteLine("{0}", line);
            }

            Console.ReadLine();
        }

        static String[] SplitOnNth(String toSplit, char splitOn, int n) {
            StringBuilder sb = new StringBuilder();
            sb.Append("(?<head>");
            for (int i = 0; i < n-1; i++) {
                sb.Append(String.Format("[^{0}]*{0}", splitOn));
            }
            sb.Append(String.Format("[^{0}]*){0}", splitOn));
            sb.Append("(?<tail>.*)");

            Regex r = new Regex(sb.ToString());
            List<String> strings = new List<string>();

            while (true) {
                Match m = r.Match(toSplit);
                GroupCollection gc = m.Groups;
                if (gc.Count > 2) {
                    strings.Add(gc["head"].Value);
                    toSplit = gc["tail"].Value;
                } else {
                    strings.Add(toSplit);
                    break;
                }
            }

            return strings.ToArray();
        }

    }
}
commented: Great stuff! +15

see the below example which can be help you. it's a just example just write your own code.

ex:- string str = "a,b,b,c,c,d,d,e,e,f,f,g,g,h,h,i,i,j,j,k,";
//now i want to split with the evry 2 comma
//so my o/p should be display like this
a,b
b,c
c,d
d,e
e,f
f,g
g,h
i,j
j,k
		for(int t=0;t<str.Length;t+=4)
		{
			if(t==str.Length){ break;}
			console.WriteLine((str.Substring(t,3) +"\n");
		}

Just like to point out that the for loop ensures that t is less than the length of the string, so there is no need for the if to see if t is equal to the length of the string.

This will invert the @Momerath list, but it's still functional:

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_412157_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         string strTest =
            "This, is, a, test, of, the, comma, splitting, thing, that," +
            "will, see, if, we, get, ten, or, not";

         int i = 0;
         int intNumPieces = 6;
         
         List<List<string>> lst_lst_strParts =
            strTest.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
            .GroupBy(s => (i++ % intNumPieces))
            .Select(s => s.ToList()).ToList();

         int intHeight = lst_lst_strParts.Count;
         int intWidth =  lst_lst_strParts[0].Count;

         for (int j = 0; j < intWidth; j++)
         {
            for (int k = 0; k < intHeight; k++)
            {
               Console.Write(lst_lst_strParts[k][j] + " ");
            }

            Console.WriteLine();
         }
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.