string length

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

    string length

    I have a string assigned a value from a querystring

    public string a = request.queryst ring[1]

    how can i make sure string a is less than 250 char? without using if's


    aaron


  • D Cameron

    #2
    Re: string length

    Unless you're willing to count the ternary operator as different from an
    if, then I can't seem to think of one.

    I'm thinking of something like this:

    (a.length < 250) ? a : a.Substring(0,2 50)

    I know it's basically an if, but you can put it inside a conditional or
    any other statement, if that's why you need to avoid ifs.

    The other string functions seem to be too exception happy to do anything
    to creative with. Same with StringBuilder.

    Dave

    Comment

    • Klaus H. Probst

      #3
      Re: string length

      public string a = Request.QuerySt ring[2].Substring(0, 250);


      --
      _______________ _____
      Klaus H. Probst, MVP
      Great prices on a large selection of domains. Find the pefect domain for your new startup.




      "Aaron" <kuya789@yahoo. com> wrote in message
      news:e1VNGQeIEH A.2236@TK2MSFTN GP10.phx.gbl...[color=blue]
      > I have a string assigned a value from a querystring
      >
      > public string a = request.queryst ring[1]
      >
      > how can i make sure string a is less than 250 char? without using if's
      >
      >
      > aaron
      >
      >[/color]


      Comment

      • D Cameron

        #4
        Re: string length

        Klaus H. Probst wrote:
        [color=blue]
        > public string a = Request.QuerySt ring[2].Substring(0, 250);[/color]

        .... if you're willing to trap the ArgumentOutOfRa ngeException for
        strings that are under 250 characters long.

        Comment

        • Hans Kesting

          #5
          Re: string length


          "Aaron" <kuya789@yahoo. com> wrote in message news:e1VNGQeIEH A.2236@TK2MSFTN GP10.phx.gbl...[color=blue]
          > I have a string assigned a value from a querystring
          >
          > public string a = request.queryst ring[1]
          >
          > how can i make sure string a is less than 250 char? without using if's
          >
          >
          > aaron
          >
          >[/color]

          and in addition to what the others said, you can also limit the input:
          make sure the textbox (if that is where the text comes from)
          accepts no more than 250 chars. (MaxLength property)

          Hans Kesting


          Comment

          • Yanick Beaudet

            #6
            Re: string length

            I would use:
            int lengthOrTruncat ed = Math.Min(Reques t.QueryString[1].Length, 250);
            string a = Request.QuerySt ring[1].Substring(0, lengthOrTruncat ed);

            (or inline the temporary int variable).

            Yanick

            Aaron wrote:
            [color=blue]
            > I have a string assigned a value from a querystring
            >
            > public string a = request.queryst ring[1]
            >
            > how can i make sure string a is less than 250 char? without using if's
            >
            >
            > aaron
            >
            >[/color]

            Comment

            • Stephen Bye

              #7
              Re: string length

              Then how about Substring(0,min (250,length))

              "D Cameron" <davcamer@hotma il.com> wrote in message
              news:%23gdQ14eI EHA.3444@TK2MSF TNGP11.phx.gbl. ..[color=blue]
              > Klaus H. Probst wrote:
              >[color=green]
              > > public string a = Request.QuerySt ring[2].Substring(0, 250);[/color]
              >
              > ... if you're willing to trap the ArgumentOutOfRa ngeException for
              > strings that are under 250 characters long.[/color]


              Comment

              • Cor Ligthert

                #8
                Re: string length

                > how can i make sure string a is less than 250 char? without using if's

                With a lot of work, by instance through looping thru the string and add
                everytime a char untill it reaches his end,

                Comparing is older in computing than multiplying, what do you think the
                computer does internally when you are not using that?

                Just my thought,

                Cor



                Comment

                • J.Marsch

                  #9
                  Re: string length

                  > and in addition to what the others said, you can also limit the input:[color=blue]
                  > make sure the textbox (if that is where the text comes from)
                  > accepts no more than 250 chars. (MaxLength property)[/color]

                  Being a paranoid developer, I don't know that I would leave it at just that,
                  though. Setting the maxlength would cover the expected scenario (where a
                  user is typing into an edit box), but it would leave you open to accepting
                  strings longer than 250 if someone just constructs a query string and posts
                  it to a URL.

                  Now, in the world of unmanaged code, if your server-side code just trusts
                  the string to be shorter than 250, and someone sneaks a larger one in by
                  building and posting their own query string, your code may be open to a
                  buffer overrun attack.

                  In the managed world, as long as you are using safe code, I don't _think_
                  that you can get a real buffer overrun. However, it still seems kind of
                  dangerous to take for granted that a string passed from an external source
                  will always conform to your size rule. I'd be suspicious and check it.


                  Comment

                  Working...