Substring

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    #1

    Substring

    If I want to get the last 4 characters of a string is this the right way
    to do it?

    int intCardLength = Convert.ToInt32 (strCardNumberU nEnc.Length);
    strCardNumberUn Enc = strCardNumberUn Enc.Substring(i ntCardLength -
    4, 4);

    Or is there stuff (Length, Substring) that is zero-indexed that will
    mean I need to take account of?






    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • timtos

    #2
    Re: Substring

    Just use it like this:
    string abc = "abcdefghijklmn opqrstuvwxyz";
    string abcEnd = abc.Substring(a bc.Length - 4);

    Hope I understood your question right...

    Greetings,
    timtos.


    "Mike P" <mrp@telcoelect ronics.co.uk> wrote in message news:uQf3CaGeDH A.2436@TK2MSFTN GP11.phx.gbl...[color=blue]
    > If I want to get the last 4 characters of a string is this the right way
    > to do it?
    >
    > int intCardLength = Convert.ToInt32 (strCardNumberU nEnc.Length);
    > strCardNumberUn Enc = strCardNumberUn Enc.Substring(i ntCardLength -
    > 4, 4);
    >
    > Or is there stuff (Length, Substring) that is zero-indexed that will
    > mean I need to take account of?
    >
    >
    >
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it![/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Substring

      Mike,

      As a general rule, when dealing with anything in .NET/C#, everything
      runs off a zero-based index (there are probably exceptions, but for the most
      part, this is true).

      The Substring method is the same. So, if you want to get the last four
      characters, you can do this:

      strCardNumberUn Enc =
      strCardNumberUn Enc.Substring(s trCardNumberUnE nc.Length - 4, 4);

      Which is basically what you had. You don't need the call to convert,
      since the Length property returns an int.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - nicholas.paldin o@exisconsultin g.com

      "Mike P" <mrp@telcoelect ronics.co.uk> wrote in message
      news:uQf3CaGeDH A.2436@TK2MSFTN GP11.phx.gbl...[color=blue]
      > If I want to get the last 4 characters of a string is this the right way
      > to do it?
      >
      > int intCardLength = Convert.ToInt32 (strCardNumberU nEnc.Length);
      > strCardNumberUn Enc = strCardNumberUn Enc.Substring(i ntCardLength -
      > 4, 4);
      >
      > Or is there stuff (Length, Substring) that is zero-indexed that will
      > mean I need to take account of?
      >
      >
      >
      >
      >
      >
      > *** Sent via Developersdex http://www.developersdex.com ***
      > Don't just participate in USENET...get rewarded for it![/color]


      Comment

      Working...