checking string for all blanks

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

    #1

    checking string for all blanks


    Could someone tell me the easiest way to check a string of variable length
    to see if it consists of all blank characters?

    ....or perhaps more generally, to see if all of the characters are the same.


    Thanks

    Jeff


    --
    Posted via a free Usenet account from http://www.teranews.com

  • Johnny Jörgensen

    #2
    Re: checking string for all blanks

    If MyString.Trim = "" Then
    ....
    End If

    Cheers,
    Johnny J.




    "Jeff" <none@nothingX. comwrote in message
    news:4642a23e$0 $1468$88260bb3@ free.teranews.c om...
    >
    Could someone tell me the easiest way to check a string of variable length
    to see if it consists of all blank characters?
    >
    ...or perhaps more generally, to see if all of the characters are the
    same.
    >
    >
    Thanks
    >
    Jeff
    >
    >
    --
    Posted via a free Usenet account from http://www.teranews.com
    >

    Comment

    • Andrew Morton

      #3
      Re: checking string for all blanks

      Jeff wrote:
      Could someone tell me the easiest way to check a string of variable
      length to see if it consists of all blank characters?
      Assuming an empty string counts as all blank chars,

      If Len(Trim(someSt ring))=0 then...
      ...or perhaps more generally, to see if all of the characters are the
      same.
      If Len(someString) >0 AndAlso Len(someString. Trim(someString .Chars(1))) = 0
      Then...

      Andrew


      Comment

      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

        #4
        Re: checking string for all blanks

        Jeff wrote:
        >
        Could someone tell me the easiest way to check a string of variable
        length to see if it consists of all blank characters?
        >
        ...or perhaps more generally, to see if all of the characters are the same.
        The most resource effecive and scalable would be to just loop the string:

        Function IsAllSameCharac ter(s As String)
        Dim first As Char = s.Chars(0)
        ForEach c As Char in s
        If c <first Return False
        Next
        Return True
        End Function

        [Disclaimer: Code is not tested]

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • Jeff

          #5
          Re: checking string for all blanks


          "Göran Andersson" <guffa@guffa.co mwrote in message
          news:uJlusPwkHH A.4312@TK2MSFTN GP02.phx.gbl...
          Jeff wrote:
          The most resource effecive and scalable would be to just loop the string:
          >
          Function IsAllSameCharac ter(s As String)
          Dim first As Char = s.Chars(0)
          ForEach c As Char in s
          If c <first Return False
          Next
          Return True
          End Function
          >
          [Disclaimer: Code is not tested]
          Other than a typo involving a missing space and one other, it ran fine.
          Thanks - my tested code is below. I don't know enough to determine whether
          this is the most resource effective relative to the other alternatives
          provided, but I will take your word for it. Jeff

          Function IsAllSameCharac ter(ByVal s As String)
          Dim first As Char = s.Chars(0)
          For Each c As Char In s
          If c <first Then
          Return False
          End If
          Next
          Return True
          End Function



          --
          Posted via a free Usenet account from http://www.teranews.com

          Comment

          • Stephany Young

            #6
            Re: checking string for all blanks

            Take his word for it that it is certainly resource effective and scalable.

            Whether it is the most ... is a matter of debate.

            For short strings it will be lightening fast.

            For longer strings, the further it has to loop through the characters the
            longer it will take and you might find it quicker to do something like:

            Function IsAllSameCharac ter(ByVal s As String)

            Dim _s As String = New String(s.Chars( 0), s.Length)

            Return (s = _s)

            End Function



            "Jeff" <none@nothingX. comwrote in message
            news:4643b29b$0 $13538$88260bb3 @free.teranews. com...
            >
            "Göran Andersson" <guffa@guffa.co mwrote in message
            news:uJlusPwkHH A.4312@TK2MSFTN GP02.phx.gbl...
            >Jeff wrote:
            >
            >The most resource effecive and scalable would be to just loop the string:
            >>
            >Function IsAllSameCharac ter(s As String)
            > Dim first As Char = s.Chars(0)
            > ForEach c As Char in s
            > If c <first Return False
            > Next
            > Return True
            >End Function
            >>
            >[Disclaimer: Code is not tested]
            >
            Other than a typo involving a missing space and one other, it ran fine.
            Thanks - my tested code is below. I don't know enough to determine whether
            this is the most resource effective relative to the other alternatives
            provided, but I will take your word for it. Jeff
            >
            Function IsAllSameCharac ter(ByVal s As String)
            Dim first As Char = s.Chars(0)
            For Each c As Char In s
            If c <first Then
            Return False
            End If
            Next
            Return True
            End Function
            >
            >
            >
            --
            Posted via a free Usenet account from http://www.teranews.com
            >

            Comment

            • =?ISO-8859-1?Q?G=F6ran_Andersson?=

              #7
              Re: checking string for all blanks

              Stephany Young wrote:
              Take his word for it that it is certainly resource effective and scalable.
              >
              Whether it is the most ... is a matter of debate.
              >
              For short strings it will be lightening fast.
              Yes, but that is not very interresting. For short strings it doesn't
              really matter. It's what happens when the strings get larger that
              matters for scalability.
              For longer strings, the further it has to loop through the characters
              the longer it will take and you might find it quicker to do something like:
              >
              Function IsAllSameCharac ter(ByVal s As String)
              >
              Dim _s As String = New String(s.Chars( 0), s.Length)
              >
              Return (s = _s)
              >
              End Function
              >
              But that will make two loops instead of one. First it will do a loop to
              initialise the new string, then it will do another loop to compare all
              the characters between the strings. At best (when all characters are the
              same) it's half as fast, at worst (when the first two characters differ)
              it's much slower, as the first loop will always loop through all the
              characters regardless if they are used or not.

              As this method creates another string that is as large as the original
              string, it does not scale well. The method that I presented only
              allocates memory for a repeater, and that is the same regardless of the
              size of the string.

              --
              Göran Andersson
              _____
              Göran Anderssons privata hemsida.

              Comment

              Working...