Splitting a string
-
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.
-
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.
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 -
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.
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
-
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
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.
-
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 Parkeri 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.
-
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.
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
-
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.
use RegExp
"When the only tool you have is a hammer, a sore thumb you will have."
-
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.
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 -