extracting substring
-
string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\\hello.txt\r\n" int x=comm.Length; x=x-7; string path; path=comm.Substring(9,x); MessageBox.Show(path); it works fine and give path as E:\projects backup\ankitclam backup\Clamtest\testing\hello.txt but when i give path string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\r\n" int x=comm.Length; x=x-7; string path; path=comm.Substring(9,x); //crashes here MessageBox.Show(path); crashes on line : path=comm.Substring(9,x); throwing exception System.Argument.OutOfRangeException:Index and length must refer to location within the string. Parameter name:length at System.String.Substring(Int32 startIndex,Int32 length) Ankit Aneja "Nothing is impossible. The word itself says - I M possible"
-
string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\\hello.txt\r\n" int x=comm.Length; x=x-7; string path; path=comm.Substring(9,x); MessageBox.Show(path); it works fine and give path as E:\projects backup\ankitclam backup\Clamtest\testing\hello.txt but when i give path string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\r\n" int x=comm.Length; x=x-7; string path; path=comm.Substring(9,x); //crashes here MessageBox.Show(path); crashes on line : path=comm.Substring(9,x); throwing exception System.Argument.OutOfRangeException:Index and length must refer to location within the string. Parameter name:length at System.String.Substring(Int32 startIndex,Int32 length) Ankit Aneja "Nothing is impossible. The word itself says - I M possible"
Both of the above blocks of code will throw an ArgumentOutofRange Exception. That's because the length you are trying to extract is more than the length of the string. The correct block of code would be: string comm = @"CONTSCAN E:\projects backup\ankitclam backup\Clamtest\testing\hello.txt\r\n" ; int x = comm.Length ; x -= 9; string path = comm.Substring(9,x) ; Hope this helps.:) Bikash Rai
-
Both of the above blocks of code will throw an ArgumentOutofRange Exception. That's because the length you are trying to extract is more than the length of the string. The correct block of code would be: string comm = @"CONTSCAN E:\projects backup\ankitclam backup\Clamtest\testing\hello.txt\r\n" ; int x = comm.Length ; x -= 9; string path = comm.Substring(9,x) ; Hope this helps.:) Bikash Rai
This is code for case I and running fine if(string.Compare("SCAN",0,comm,0,4)==0) { int x=comm.Length; x=x-7; string path; path=comm.Substring(5,x); path=path.Trim(); } //here comm is having value //SCAN E:\projects backup\ankitclam backup\Clamtest\testing\hello.txt\r\n and path=@"E:\projects backup\ankitclam backup\Clamtest\testing\hello.txt" and also works when comm = SCAN E:\projects backup\ankitclam backup\Clamtest\testing\r\n thanks for ur help i am checking for contscan Ankit Aneja "Nothing is impossible. The word itself says - I M possible"
-
This is code for case I and running fine if(string.Compare("SCAN",0,comm,0,4)==0) { int x=comm.Length; x=x-7; string path; path=comm.Substring(5,x); path=path.Trim(); } //here comm is having value //SCAN E:\projects backup\ankitclam backup\Clamtest\testing\hello.txt\r\n and path=@"E:\projects backup\ankitclam backup\Clamtest\testing\hello.txt" and also works when comm = SCAN E:\projects backup\ankitclam backup\Clamtest\testing\r\n thanks for ur help i am checking for contscan Ankit Aneja "Nothing is impossible. The word itself says - I M possible"
This obviously will work because the startpoint that you mention in Substring is less than the value you subtract from the length of the string. In your previous post it was x=x-7; string path; path=comm.Substring(9,x); which will definately not work, but now its x=x-7; string path; path=comm.Substring(5,x); which will always work. :) Seems like you are messing up the numbers. If you want the above to work with CONTSCAN then it should be something like string comm = "CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\r\n" ; int x=comm.Length ; x -= 11 ; string path; path=comm.Substring(9,x); path=path.Trim(); You must subtract 11 from the string and not 7 because "CONTSCAN " is 9 and "\r\n" is 2. So 9 + 2 = 11.;) Hope this helps.:) Bikash Rai
-
string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\\hello.txt\r\n" int x=comm.Length; x=x-7; string path; path=comm.Substring(9,x); MessageBox.Show(path); it works fine and give path as E:\projects backup\ankitclam backup\Clamtest\testing\hello.txt but when i give path string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\r\n" int x=comm.Length; x=x-7; string path; path=comm.Substring(9,x); //crashes here MessageBox.Show(path); crashes on line : path=comm.Substring(9,x); throwing exception System.Argument.OutOfRangeException:Index and length must refer to location within the string. Parameter name:length at System.String.Substring(Int32 startIndex,Int32 length) Ankit Aneja "Nothing is impossible. The word itself says - I M possible"
Hi, Change your code like this...
string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\\hello.txt\r\n"; /* int x=comm.Length; x=x-9; string path; path=comm.Substring(9,x); MessageBox.Show(path); */ comm = comm.Replace("CONTSCAN ",""); comm = comm.Replace("\r\n",""); System.Diagnostics.Debug.WriteLine(comm);
Happy Programming!!! :) Regards, P.Anbuselvan Sr. Software Engineer Hyderabad -
This obviously will work because the startpoint that you mention in Substring is less than the value you subtract from the length of the string. In your previous post it was x=x-7; string path; path=comm.Substring(9,x); which will definately not work, but now its x=x-7; string path; path=comm.Substring(5,x); which will always work. :) Seems like you are messing up the numbers. If you want the above to work with CONTSCAN then it should be something like string comm = "CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\r\n" ; int x=comm.Length ; x -= 11 ; string path; path=comm.Substring(9,x); path=path.Trim(); You must subtract 11 from the string and not 7 because "CONTSCAN " is 9 and "\r\n" is 2. So 9 + 2 = 11.;) Hope this helps.:) Bikash Rai
thanks it worked with following code if(string.Compare("CONTSCAN",0,comm,0,8)==0) { int x=comm.Length; x=x-9; string path; path=comm.Substring(9,x); path=path.Trim(); result=res.loadDatabase("CONTSCAN",path); } Ankit Aneja "Nothing is impossible. The word itself says - I M possible"
-
Hi, Change your code like this...
string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\\hello.txt\r\n"; /* int x=comm.Length; x=x-9; string path; path=comm.Substring(9,x); MessageBox.Show(path); */ comm = comm.Replace("CONTSCAN ",""); comm = comm.Replace("\r\n",""); System.Diagnostics.Debug.WriteLine(comm);
Happy Programming!!! :) Regards, P.Anbuselvan Sr. Software Engineer Hyderabadthanks Ankit Aneja "Nothing is impossible. The word itself says - I M possible"
-
Hi, Change your code like this...
string comm="CONTSCAN E:\\projects backup\\ankitclam backup\\Clamtest\\testing\\hello.txt\r\n"; /* int x=comm.Length; x=x-9; string path; path=comm.Substring(9,x); MessageBox.Show(path); */ comm = comm.Replace("CONTSCAN ",""); comm = comm.Replace("\r\n",""); System.Diagnostics.Debug.WriteLine(comm);
Happy Programming!!! :) Regards, P.Anbuselvan Sr. Software Engineer HyderabadI totally agree. :) Bikash Rai