Results 1 to 8 of 8

Thread: [RESOLVED] Shorten code

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Shorten code

    Ola,

    I want a textbox to be checked for specific characters. I can use:
    Code:
    with textbox1.text
        If .contains("a") or .contains("b") or .contains("c")...etc... then...
    How can I shorten this?


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Shorten code

    Code:
    If text.IndexOfAny(New Char() {"a"c, "b"c, "c"c}) > -1 Then

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Shorten code

    You can use the String.IndexOf (Any) to give you the loction of any of those characters and bail. Example here.

  4. #4

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Shorten code

    Thank you both. Will have a look at it.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  5. #5

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Shorten code

    Had to figure out why it kept saying that there was an illegal character although it wasn't.
    "If text.IndexOfAny(New Char() {"a"c, "b"c, "c"c}) > -1 Then" means something like: When not one of the characters found in the index of the text".

    If someone knows how to formulate it better, please let me know.
    +REP btw.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Shorten code

    I can only guess that you want to run the If when you DO NOT have a value that matches, so just change it.

    IndexOfAny.... = -1

  7. #7

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Shorten code

    I figured that already m8. "issue" was solved already. Thanks again.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: [RESOLVED] Shorten code

    These are just some examples:
    Code:
    Dim text = "the quick brown fox!"
    Dim letters = "abc".ToCharArray()
    
    If Not text.ContainsAny("a"c, "b"c, "c"c) Then
    
    End If
    '//or
    If Not text.ContainsAny(letters) Then
    
    End If
    '//or
    If Not text.IndexOfAny(letters) > -1 Then
    
    End If
    
    Public Module StringExtensions
    
        ''' <summary>
        ''' Returns a value indicating whether any of the specified <see cref="System.Char" />
        ''' objects occur within this string.
        ''' </summary>
        ''' <param name="value">The value whose characters to check.</param>
        ''' <param name="chars">The <see cref="System.Char" /> objects to seek.</param>
        <Runtime.CompilerServices.Extension()> _
        Public Function ContainsAny(ByVal value As String, _
                                    ByVal ParamArray chars() As Char) As Boolean
            Return value.IndexOfAny(chars) > -1
        End Function
    
    End Module

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