checking for spaces

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

    checking for spaces

    How do I check for spaces in a string variable? Nothing but spaces or
    nothing.
    I am using
    if trim(x) = "" then
    which works but I was wondering if there was a cleaner way to do it.

    I prefer not using variant variables.

    Thank you
    Smokey
  • Rick Rothstein

    #2
    Re: checking for spaces

    > How do I check for spaces in a string variable? Nothing[color=blue]
    > but spaces or nothing. I am using
    > if trim(x) = "" then
    > which works but I was wondering if there was a cleaner
    > way to do it.[/color]

    I can't imagine anything that would be too much "cleaner" than that.
    Here are two alternatives to using the Trim function (neither of which
    are necessarily "cleaner"). ..

    If Not X Like "*[! ]*" Then

    or

    If X = Space$(Len(X)) Then

    [color=blue]
    > I prefer not using variant variables.[/color]

    If that is the case, then you should put a $ sign after the keyword
    "Trim", like this...

    If Trim$(X) = "" Then

    With the $ sign, Trim returns a String value directly; without the $
    sign, Trim returns a Variant with a sub-type of String.


    Rick - MVP

    Comment

    • preben nielsen

      #3
      Re: checking for spaces


      "Smokey Joe" <oldsmokee@yaho o.com> skrev i en meddelelse
      news:5jrll0t8r2 0c5tqbuk6e8cgf4 cg394a7df@4ax.c om...[color=blue]
      > How do I check for spaces in a string variable? Nothing but
      > spaces or
      > nothing.
      > I am using
      > if trim(x) = "" then[/color]

      As clean as it gets.....

      Or this......

      IF EmptyString(X) THEN


      Function EmptyString(str as string) as Boolean
      EmptyString = (Trim(str) = "")
      End Function

      Build a library of support-functions that suits you....


      --
      /\ preben nielsen
      \/\ prel@post.tele. dk


      Comment

      • Steve Gerrard

        #4
        Re: checking for spaces


        "Smokey Joe" <oldsmokee@yaho o.com> wrote in message
        news:5jrll0t8r2 0c5tqbuk6e8cgf4 cg394a7df@4ax.c om...
        | How do I check for spaces in a string variable? Nothing but spaces or
        | nothing.
        | I am using
        | if trim(x) = "" then
        | which works but I was wondering if there was a cleaner way to do it.
        |
        | I prefer not using variant variables.
        |
        | Thank you
        | Smokey

        As already said, that's pretty clean.

        I would use
        If Len(Trim(X)) = 0 Then
        just because - um, I guess because it makes more sense to me.


        Comment

        • Rick Rothstein

          #5
          Re: checking for spaces

          > | How do I check for spaces in a string variable? Nothing but spaces
          or[color=blue]
          > | nothing.
          > | I am using
          > | if trim(x) = "" then
          > | which works but I was wondering if there was a cleaner way to do it.
          > |
          > | I prefer not using variant variables.
          > |
          > | Thank you
          > | Smokey
          >
          > As already said, that's pretty clean.
          >
          > I would use
          > If Len(Trim(X)) = 0 Then
          > just because - um, I guess because it makes more sense to me.[/color]

          It is also faster than comparing String values directly (although I
          would still throw the $ sign on the end of the Trim keyword in order to
          force the Trim function to return a String value directly (otherwise,
          without the $ sign, Trim returns a Variant with a sub-type of String).

          Rick - MVP

          Comment

          • Smokey Joe

            #6
            Re: checking for spaces

            Thank you to everyone.

            There is a language where you can say if x = spaces etc.
            To me that seems "clean".

            Smokey

            Comment

            • Smokey Joe

              #7
              Re: checking for spaces

              Rick

              I am sitting hear searching all of the docs that came with vb6 and
              msdn and cannot find anything that explains the difference between
              trim and trim$ or any of the ...$ functions or statements.

              Where would I find the explanation? Are there other differences
              besides returning a string vs a variant(string)

              Thank you
              Smokey

              Comment

              • build

                #8
                Re: checking for spaces

                Smokey Joe wrote:
                [color=blue]
                > Thank you to everyone.
                >
                > There is a language where you can say if x = spaces etc.
                > To me that seems "clean".
                >
                > Smokey[/color]

                G'day Smokey,
                seems silly but ...
                If x = " " then ...

                For i = 1 to len(myString)
                x = mid(myString,i, 1)
                If x = " " then
                myString = Replace(myStrin g, " ", "")
                end if
                next i

                untested but you'll figure it out, I hope.
                in VB select a keyword i.e. Replace then press F1.
                cheers
                build

                Comment

                Working...