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. i incounter substring problem

i incounter substring problem

Scheduled Pinned Locked Moved C#
helpquestion
7 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.
  • P Offline
    P Offline
    paulcortez
    wrote on last edited by
    #1

    Hello guys, I'm halfway of my program when I encounter a problem. i'm trying to substring "test" string by this command(sample only): string strTest = "test"; MessageBox.Show(strTest.SubString(2, 3)); and I get this result: t My desired result is: st but I got my desired result with this code: string strTest = "test"; MessageBox.Show(strTest.SubString(2, 2)); I can't get why this happened. Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right? Thank you in advance...:)

    D M B 3 Replies Last reply
    0
    • P paulcortez

      Hello guys, I'm halfway of my program when I encounter a problem. i'm trying to substring "test" string by this command(sample only): string strTest = "test"; MessageBox.Show(strTest.SubString(2, 3)); and I get this result: t My desired result is: st but I got my desired result with this code: string strTest = "test"; MessageBox.Show(strTest.SubString(2, 2)); I can't get why this happened. Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right? Thank you in advance...:)

      D Offline
      D Offline
      Daniele Ferrero
      wrote on last edited by
      #2

      t e s t 0 1 2 3 SubString(initial Position , lenght) SubString(2, 2) is ok for get "st" with SubString(2,3) you got an exception (System.ArgumentOutOfRangeException)

      J 1 Reply Last reply
      0
      • D Daniele Ferrero

        t e s t 0 1 2 3 SubString(initial Position , lenght) SubString(2, 2) is ok for get "st" with SubString(2,3) you got an exception (System.ArgumentOutOfRangeException)

        J Offline
        J Offline
        johan3131
        wrote on last edited by
        #3

        I think what he means is that SubString parameters are not begin and end position (like in Java), rather begining position and how much further from there, Ex Test 0123 SubString(2,2) - Te and two place further gives s and t. Third is out of arrays so System.ArgumentOutOfRangeException is thrown

        P 1 Reply Last reply
        0
        • P paulcortez

          Hello guys, I'm halfway of my program when I encounter a problem. i'm trying to substring "test" string by this command(sample only): string strTest = "test"; MessageBox.Show(strTest.SubString(2, 3)); and I get this result: t My desired result is: st but I got my desired result with this code: string strTest = "test"; MessageBox.Show(strTest.SubString(2, 2)); I can't get why this happened. Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right? Thank you in advance...:)

          M Offline
          M Offline
          Mike Poz
          wrote on last edited by
          #4

          paulcortez wrote:

          Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right?

          Second parameter is number of characters in the string you want to display. If you want to display the last two characters in the string, leave off the second parameter. If you need to display a certain number of characters starting at 2 and you want to be sure it doesn't hit the out of range check the starting character plus number of chars you want to display against the strTest.Length value. If the starting plus number to display exceeds length, then you're out of bounds. Mike Poz

          P 1 Reply Last reply
          0
          • P paulcortez

            Hello guys, I'm halfway of my program when I encounter a problem. i'm trying to substring "test" string by this command(sample only): string strTest = "test"; MessageBox.Show(strTest.SubString(2, 3)); and I get this result: t My desired result is: st but I got my desired result with this code: string strTest = "test"; MessageBox.Show(strTest.SubString(2, 2)); I can't get why this happened. Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right? Thank you in advance...:)

            B Offline
            B Offline
            Bogdan Damian
            wrote on last edited by
            #5

            public string Substring( int startIndex, int length ); damianbc

            1 Reply Last reply
            0
            • J johan3131

              I think what he means is that SubString parameters are not begin and end position (like in Java), rather begining position and how much further from there, Ex Test 0123 SubString(2,2) - Te and two place further gives s and t. Third is out of arrays so System.ArgumentOutOfRangeException is thrown

              P Offline
              P Offline
              paulcortez
              wrote on last edited by
              #6

              Guys,,, Thank you so much. Now I understand that the second parameter is the lenght, not the position of the character where you want to end. Thankz so much..

              1 Reply Last reply
              0
              • M Mike Poz

                paulcortez wrote:

                Code was supposed to be MessageBox.Show(strTest.SubString(2, 3)); right?

                Second parameter is number of characters in the string you want to display. If you want to display the last two characters in the string, leave off the second parameter. If you need to display a certain number of characters starting at 2 and you want to be sure it doesn't hit the out of range check the starting character plus number of chars you want to display against the strTest.Length value. If the starting plus number to display exceeds length, then you're out of bounds. Mike Poz

                P Offline
                P Offline
                paulcortez
                wrote on last edited by
                #7

                Guys,,, Thank you so much. Now I understand that the second parameter is the lenght, not the position of the character where you want to end. Thankz so much..

                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