Results 1 to 9 of 9

Thread: [02/03] INSTR() Function

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Question [02/03] INSTR() Function

    Hey all. How can I use INSTR to find out if a string starts at the beginning of another string...
    for example if it were to search for "http://" in "http://www.blah.com" it would find it, however if it were to search "blahhttp://www.com" it would not find it since it's not at the beginning.

    I would like to use it in an if statement.. If not found then blah end if

    I'm sure this is easy and I'm just missing something, please help! Thanks in advance

  2. #2
    Member
    Join Date
    Sep 2005
    Posts
    49

    Re: [02/03] INSTR() Function

    for .net use the string.contains or instr(string,pattern) <> 0

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Re: [02/03] INSTR() Function

    Quote Originally Posted by wrecklesswun
    for .net use the string.contains or instr(string,pattern) <> 0
    Yeah but that only searches for string1 anywhere in the string2. Is there a way I can find out if string1 is the beginning of string2? Or another way of putting, if string2 begins with string1

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

    Re: [02/03] INSTR() Function

    Don't use Runtime functions unless they add value. InStr doesn't. You want to do something with a String object, you go to the MSDN help topic for the member listing of the String class. There you find StartsWith, EndsWith and IndexOf. Contains is new in .NET 2.0 and can be replaced in .NET 1.x with IndexOf <> -1
    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

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [02/03] INSTR() Function

    Use a combination of IndexOf() and SubString() which are both methods of the string object.

    IndexOf() returns an integer value of where in the string, the specified substring is found. It returns the index of the first character in the found string.

    SubString() is used to return a portion of a given string.

    So once you know where in the string the substring you are looking for starts (by using indexof) you can then use substring to pull out the string you want.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    7

    Re: [02/03] INSTR() Function

    Thanks for the replies!! The String.StartsWith worked great!!!

  7. #7
    Member
    Join Date
    Sep 2006
    Posts
    63

    Re: [02/03] INSTR() Function

    Quote Originally Posted by jmcilhinney
    Don't use Runtime functions unless they add value. InStr doesn't. You want to do something with a String object.
    What do you mean by a runtime function?

    What are the benefits from using the string object?

    I assume VAL() is a runtime function. So how should I rewrite something like:
    intAnswer = VAL(strA) + VAL(strB)

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

    Re: [02/03] INSTR() Function

    Quote Originally Posted by ghall426
    What do you mean by a runtime function?

    What are the benefits from using the string object?

    I assume VAL() is a runtime function. So how should I rewrite something like:
    intAnswer = VAL(strA) + VAL(strB)
    I mean functions that rae part of the VB.NET Runtime, which basically means all the VB6 functions carried over into VB.NET so that (a) upgraded VB6 code will still work and (b) VB6 developers don't get grumpy because they have to type MessageBox.Show instead of MsgBox. Most Runtime functions have an alternative in a System-based namespace that is as good or better, so using VB6-style code just doesn't make sense.

    There are certain situations where a Runtime function can add value and Val() can be one of those, although not always. For instance, the .NET Framework provides numerous ways to convert a string to a number and they should be used in preference in most situations. An occasion where Val() is useful is when you have a string like "27 kg" and you want to get just the number from the beginning. Val() will do that in one line where System-based types and members would take several. Having said that, Val() is vastly INFERIOR in many other situations. For instance, let's say that the user enters a numerical value in a TextBox and you want to convert that to a number and save it. If the user was to accidentally input "123o456" then Val() would convert that to 123 and you'd be saving completely the wrong value when the user meant 1230456. You could use IsNumeric to test it first but guess what: IsNumeric calls Double.TryParse internally. What you should do is call the TryParse method of the appropriate numeric type explicitly.
    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

  9. #9
    Member
    Join Date
    Sep 2005
    Posts
    49

    Re: [02/03] INSTR() Function

    it all boils down to the same low level machine call as well, correct? either the using the .net framework, you are steps removed from the processor or at least the os. Native code is the only direct method next to assembly

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