CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: String variable

  1. #1
    Join Date
    Jun 2001
    Location
    Michigan
    Posts
    2,222

    String variable

    How can I determine if a String variable contains a valid string? For numeric variables I can use IsNumeric, is there something similar for text?

  2. #2
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651

    Re: String variable

    What do you consider a "valid string"? You could check for characters that you don't want or characters that you must have by using InStr.
    I'd rather be wakeboarding...

  3. #3
    Join Date
    Jun 2001
    Location
    Michigan
    Posts
    2,222

    Re: String variable

    I would consider a valid string anything containing ascii codes 32 - 126.

    1) How do I traverse through each letter in a string?
    2) How do I convert a letter to an ascii code in vb?

    My vb is a little rough I'm used to c++.

  4. #4
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Re: String variable

    You could try the following:

    Code:
    dim x as integer
    dim strCharacter as string
    dim lglValidString as boolean
    
    for x = 1 to len(string) 
       strCharacter = mid$(string,x,1)
       if asc(strCharacter) > 31 or asc(strCharacter) < 127 then
            lglValidString = True
       Else
            lglValidString = False
            Exit For
       End If
    Next
    
    If Not lglValid String Then
    ......
    End If
    HTH

    JP
    JP

    Please remember to rate all postings.

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: String variable

    the following function replaces anything that is not in the range 0 to 9 or 'a' to 'z' with a minus sign:
    Code:
    Private Sub cleanseBackupComment()
      backupComment = LCase$(backupComment)
      backupComment = Replace$(backupComment, " ", "_")
      'clean the comment up.. replace all chars not in 0 - 9 to a - z with a minus
      Dim charNum As Integer
      
      'up to 0
      For charNum = &H0 To &H2F
        backupComment = Replace$(backupComment, Chr$(charNum), "-")
      Next charNum
      'from 9 to underscore (5F)
      For charNum = &H3A To &H5E
        backupComment = Replace$(backupComment, Chr$(charNum), "-")
      Next charNum
      backupComment = Replace$(backupComment, Chr$(&H60), "-")
      'from z to 255
      For charNum = &H7B To &HFF
        backupComment = Replace$(backupComment, Chr$(charNum), "-")
      Next charNum
    End Sub
    the opposite of Chr$() is Asc()

    supply chr with a number, it gives a character. supply chr$ with a number, it gives a string of that character, supply asc with a one char string and it gives a number of that string/char:

    Chr$(32) -> " "
    Asc(" ") -> 32

    asc only works on the first given character, so Asc(" abc") only gives 32 for the initial space

    AscW is the Unicode version
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Jun 2001
    Location
    Michigan
    Posts
    2,222

    Re: String variable

    Thanks that's exactly what I was looking for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured