Results 1 to 6 of 6

Thread: StartsWith/EndsWith vs Like

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2021
    Posts
    37

    StartsWith/EndsWith vs Like

    Is there any difference between following commands?

    Code:
    If mwd.EndsWith("ous") Then mwd = mwd.Replace("ous","ás")
    Code:
    If mwd Like "*ous" Then mwd = mwd.Replace("ous","ás")

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: StartsWith/EndsWith vs Like

    Specifically, yes. Practically, no. Personally, I never use the VB Like operator. You'll likely find that most who use C# as well don't. I'd suggest you go with the former but, whatever you do choose, be consistent. The fact that the former is the same in C# and is therefore consistent is one reason I do use it.

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2021
    Posts
    37

    Re: StartsWith/EndsWith vs Like

    Intesting the situation with C#.

    I find LIKE more versatile because I can compare like words ending with cise or cize in one command LIKE "*ci[sz]e".

    The corresponding code with EndsWith will be more complex to write!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: StartsWith/EndsWith vs Like

    Quote Originally Posted by mobi12 View Post
    Intesting the situation with C#.

    I find LIKE more versatile because I can compare like words ending with cise or cize in one command LIKE "*ci[sz]e".

    The corresponding code with EndsWith will be more complex to write!
    You wouldn't use EndsWith for that in C# but rather a Regex. Regular expressions are more versatile still, but do need a bit of work to learn. Like a I said, just be consistent. If you consistently want to use the simplest option then that would be EndsWith where it is possible, otherwise Like where it is possible, otherwise Regex. Writing the most readable code should always be one of your highest priorities and EndsWith is more self-documenting than Like so that's a point in its favour where it can be used.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: StartsWith/EndsWith vs Like

    Something to consider is the following statement from the Like operator's documentation: https://docs.microsoft.com/en-us/dot.../like-operator
    The Like operator is currently not supported in .NET Core and .NET Standard projects.
    Something else to consider, back to JMcIlhinney's comments on consistency, is that the Like operator uses a limited pattern syntax that isn't a one-to-one match to its closest (more widespread) alternative RegEx. For example, the pattern for a character not in range for the Like operator is [!abc] whereas the pattern in RegEx is [^abc].

    Speaking to your specific "ends with cise or cize" example, here is an alternative using RegEx:
    Code:
    ci[sz]e$
    Usage in Visual Basic .NET:
    Code:
    Dim pattern = New Regex("ci[sz]e$")
    pattern.IsMatch("string to test")
    Fiddle: https://dotnetfiddle.net/RLkE0A
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: StartsWith/EndsWith vs Like

    I would just use a regex replace and not do the check...
    Also with your examples if they end with ous every occurrence of ous in the string will be replaced!

    With the $ at the end in your pattern only the one at the end of the string will get replaced.

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