Results 1 to 5 of 5

Thread: How to check if a string contains "

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    34

    How to check if a string contains "

    I'm writing a little function to interact with a user inputting length in a form like 1'-11" Or something similar. I need to verify that both ' and " are used. However... I can't figure out how to get vb.net to recognize that I want to check if " is used and not use " to bracket a string.

    Code:
    For i = 0 To sString.Count - 1
                If Not sString(i) = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Or "7" Or "8" Or "9" Or "0" Or "." Or "-" Or "'" Or "" Then
                    'String is incorrect format, show msgbox error
                End If
            Next
    I need to add Or """

    Any thoughts?

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: How to check if a string contains "

    For a " you use "".
    If TheString.Contains("""") Then
    Or if you need to do it your way:
    If Not sString(i) = """" Then
    VB 2005, Win Xp Pro sp2

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2007
    Posts
    34

    Re: How to check if a string contains "

    Awesome thanks.

  4. #4
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    Re: How to check if a string contains "

    Could you also use chr(34) & chr(34)

    THat's the character code for a "

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to check if a string contains "

    If you want to display double quotes in string literals then use a second double quote to escape it, e.g.
    vb.net Code:
    1. Dim str As String = "He said ""Hello"""
    If you want to refer to the double quote character specifically then use ControlChars.Quote, e.g.
    vb.net Code:
    1. For Each ch As Char In sString
    2.     If Not Char.IsDigit(ch) AndAlso ch <> ControlChars.Quote Then
    3.         'String is not in correct format.
    4.         Exit For
    5.     End If
    6. Next
    There's never a reason to use Chr(34) in VB.NET as was done in VB6.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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