Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Splitting a string

Splitting a string

Scheduled Pinned Locked Moved C#
career
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 508833
    wrote on last edited by
    #1

    if i have a string with multiple lines in it like this: 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... how would i split it into chunks from the '0' lines to the '0' lines, like this: **Chunk 1** 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ **Chunk 2** 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ **Chunk 3** 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... etc. thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

    N A P D 4 Replies Last reply
    0
    • U User 508833

      if i have a string with multiple lines in it like this: 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... how would i split it into chunks from the '0' lines to the '0' lines, like this: **Chunk 1** 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ **Chunk 2** 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ **Chunk 3** 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... etc. thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      When reading one line at a time check the value of the first character, if it is '0' you have reached your break point, else append it (a StringBuilder object would work nicely here). There are articles available for reading files in C# if this is what you are asking about. HTH. :) -Nick Parker

      U 1 Reply Last reply
      0
      • U User 508833

        if i have a string with multiple lines in it like this: 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... how would i split it into chunks from the '0' lines to the '0' lines, like this: **Chunk 1** 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ **Chunk 2** 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ **Chunk 3** 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... etc. thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

        A Offline
        A Offline
        A Wegierski
        wrote on last edited by
        #3

        Search for "\n0 @" in string (or use something faster like StringBuilder). Like this (check syntax, not tested): string multiple=" ... "; ArrayList chunks=new ArrayList(1); while(multiple!="") { int p=multiple.IndexOf("\n0 @"); if(p<0) { chunks.Add(multiple); break; } chunks.Add(multiple.Substring(0,p)); multiple=multiple.Substring(p+1); } Hi, AW

        U 1 Reply Last reply
        0
        • A A Wegierski

          Search for "\n0 @" in string (or use something faster like StringBuilder). Like this (check syntax, not tested): string multiple=" ... "; ArrayList chunks=new ArrayList(1); while(multiple!="") { int p=multiple.IndexOf("\n0 @"); if(p<0) { chunks.Add(multiple); break; } chunks.Add(multiple.Substring(0,p)); multiple=multiple.Substring(p+1); } Hi, AW

          U Offline
          U Offline
          User 508833
          wrote on last edited by
          #4

          could i use the string.split method and use "\n0 @" as the delimeter? btw, what does the @ mean in '\n0 @'? thanks for the help, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

          A 1 Reply Last reply
          0
          • N Nick Parker

            When reading one line at a time check the value of the first character, if it is '0' you have reached your break point, else append it (a StringBuilder object would work nicely here). There are articles available for reading files in C# if this is what you are asking about. HTH. :) -Nick Parker

            U Offline
            U Offline
            User 508833
            wrote on last edited by
            #5

            i started out by reading each line one by one with a streamreader but i didn't want to access the file everytime i needed it so i read the whole thing at once and dumped it into a string. now i just need to figure out how to split the string up. i'll look into the stringbuilder class though. thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

            1 Reply Last reply
            0
            • U User 508833

              could i use the string.split method and use "\n0 @" as the delimeter? btw, what does the @ mean in '\n0 @'? thanks for the help, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

              A Offline
              A Offline
              A Wegierski
              wrote on last edited by
              #6

              1. No. Unfortunately Split method uses chars ... You must use above one character "\n0" 2. All Your "0" groups starts with " @" after "0". It is unnecessary, new line and zero will be enough. Hi, AW

              1 Reply Last reply
              0
              • U User 508833

                if i have a string with multiple lines in it like this: 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... how would i split it into chunks from the '0' lines to the '0' lines, like this: **Chunk 1** 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ **Chunk 2** 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ **Chunk 3** 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... etc. thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

                P Offline
                P Offline
                Philip Fitzsimons
                wrote on last edited by
                #7

                use RegExp


                "When the only tool you have is a hammer, a sore thumb you will have."

                1 Reply Last reply
                0
                • U User 508833

                  if i have a string with multiple lines in it like this: 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... how would i split it into chunks from the '0' lines to the '0' lines, like this: **Chunk 1** 0 @IQLNP-M7@ INDI 1 NAME Benoni /THOMPSON/ 2 GIVN Benoni 2 SURN THOMPSON 1 AFN QLNP-M7 1 SEX M 1 SOUR @S01@ 1 BIRT 2 DATE 14 Apr 1782 2 PLAC Alstead, N.h. 1 DEAT 2 DATE 24 Oct 1857 2 PLAC Shalersville, N.h. 1 FAMS @F6073064@ 1 FAMC @F6073065@ **Chunk 2** 0 @IQLNP-KV@ INDI 1 NAME Job /THOMPSON/ 2 GIVN Job 2 SURN THOMPSON 1 AFN QLNP-KV 1 SEX M 1 SOUR @S01@ 1 FAMS @F6073065@ **Chunk 3** 0 @IQLNP-L2@ INDI 1 NAME Lovice /CRANE/ 2 GIVN Lovice 2 SURN CRANE 1 AFN QLNP-L2 1 SEX F 1 SOUR @S01@ 1 FAMS @F6073065@ ... etc. thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

                  D Offline
                  D Offline
                  Donald_a
                  wrote on last edited by
                  #8

                  This seems to work - i'm sure it can be optimised though:

                  			//Your string
                  			string s = "0 @IQLNP-M7@ INDI" + Environment.NewLine;
                  			s += "1 NAME Benoni /THOMPSON/" + Environment.NewLine;
                  			s += "2 GIVN Benoni" + Environment.NewLine;
                  			s += "2 SURN THOMPSON" + Environment.NewLine;
                  			s += "1 AFN QLNP-M7" + Environment.NewLine;
                  			s += "1 SEX M" + Environment.NewLine;
                  			s += "1 SOUR @S01@" + Environment.NewLine;
                  			s += "1 BIRT" + Environment.NewLine;
                  			s += "2 DATE 14 Apr 1782" + Environment.NewLine;
                  			s += "2 PLAC Alstead, N.h." + Environment.NewLine;
                  			s += "1 DEAT" + Environment.NewLine;
                  			s += "2 DATE 24 Oct 1857" + Environment.NewLine;
                  			s += "2 PLAC Shalersville, N.h." + Environment.NewLine;
                  			s += "1 FAMS @F6073064@" + Environment.NewLine;
                  			s += "1 FAMC @F6073065@" + Environment.NewLine;
                  			s += "0 @IQLNP-KV@ INDI" + Environment.NewLine;
                  			s += "1 NAME Job /THOMPSON/" + Environment.NewLine;
                  			s += "2 GIVN Job" + Environment.NewLine;
                  			s += "2 SURN THOMPSON" + Environment.NewLine;
                  			s += "1 AFN QLNP-KV" + Environment.NewLine;
                  			s += "1 SEX M" + Environment.NewLine;
                  			s += "1 SOUR @S01@" + Environment.NewLine;
                  			s += "1 FAMS @F6073065@" + Environment.NewLine;
                  			s += "0 @IQLNP-L2@ INDI" + Environment.NewLine;
                  			s += "1 NAME Lovice /CRANE/" + Environment.NewLine;
                  			s += "2 GIVN Lovice" + Environment.NewLine;
                  			s += "2 SURN CRANE" + Environment.NewLine;
                  			s += "1 AFN QLNP-L2" + Environment.NewLine;
                  			s += "1 SEX F" + Environment.NewLine;
                  			s += "1 SOUR @S01@" + Environment.NewLine;
                  			s += "1 FAMS @F6073065@" + Environment.NewLine;
                  
                  			//We are going to make our way through the string looking for Environment.NewLine + "0 "
                  			//this is our current index.
                  			int index = 0;
                  			//To hold indexes of found strings
                  			System.Collections.ArrayList indexList = new System.Collections.ArrayList();
                  			//Start with zero
                  			indexList.Add(0);
                  			//Loop until none found
                  			while (index != -1)
                  			{
                  				//Look for index
                  				index = s.IndexOf(Environment.NewLine + "0 ", index + 1, s.Length - (index + 1));		
                  				
                  				//Add if found
                  				if(index != -1)
                  				{
                  					indexList.Add(index);	
                  				}
                  			}
                  
                  			//string array to, hold split strings
                  			string[] splitStrings = new string[indexList.Count];
                  			
                  			//Loop over our indexes
                  			for(int i = 0 ; i < indexList.Count ; i++)
                  			{
                  				//if ots the last one - get string to end
                  				if(i == indexList.Count - 1)
                  				{
                  					splitStrings[i] = s.Substring(Convert.ToInt32(indexList[i]), s.Length -
                  
                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups