string length

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

    #1

    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

    • D Cameron

      #3
      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

        #4
        Re: string length

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


        --
        _______________ _____
        Klaus H. Probst, MVP




        "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

        • Klaus H. Probst

          #5
          Re: string length

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


          --
          _______________ _____
          Klaus H. Probst, MVP




          "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

            #6
            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

            • D Cameron

              #7
              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

                #8
                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

                • Hans Kesting

                  #9
                  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

                    #10
                    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

                    • Yanick Beaudet

                      #11
                      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

                        #12
                        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

                        • Stephen Bye

                          #13
                          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

                            #14
                            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

                            • Cor Ligthert

                              #15
                              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

                              Working...