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. extracting substring

extracting substring

Scheduled Pinned Locked Moved C#
databasetestingbeta-testing
8 Posts 3 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.
  • A Offline
    A Offline
    Ankit Aneja
    wrote on last edited by
    #1

    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"

    H A 2 Replies Last reply
    0
    • A Ankit Aneja

      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"

      H Offline
      H Offline
      HakunaMatada
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • H HakunaMatada

        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

        A Offline
        A Offline
        Ankit Aneja
        wrote on last edited by
        #3

        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"

        H 1 Reply Last reply
        0
        • A Ankit Aneja

          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"

          H Offline
          H Offline
          HakunaMatada
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • A Ankit Aneja

            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"

            A Offline
            A Offline
            Anbuselvan
            wrote on last edited by
            #5

            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

            A H 2 Replies Last reply
            0
            • H HakunaMatada

              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

              A Offline
              A Offline
              Ankit Aneja
              wrote on last edited by
              #6

              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"

              1 Reply Last reply
              0
              • A Anbuselvan

                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

                A Offline
                A Offline
                Ankit Aneja
                wrote on last edited by
                #7

                thanks Ankit Aneja "Nothing is impossible. The word itself says - I M possible"

                1 Reply Last reply
                0
                • A Anbuselvan

                  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

                  H Offline
                  H Offline
                  HakunaMatada
                  wrote on last edited by
                  #8

                  I totally agree. :) Bikash Rai

                  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