Solved

VB.NET string function

Posted on 2004-09-01
7
452 Views
Last Modified: 2010-08-05
I need a function that takes in two strings(first string is a sentence and second is a single word) and returns true if the first string contains the second string and false otherwise.
For example MyFunction("This is my test string", "that") should return false and MyFunction("This is my test string", "test") returns true
0
Comment
Question by:YZlat
7 Comments
 
LVL 1

Accepted Solution

by:
deane_barker earned 80 total points
ID: 11957059
This is the classis InStr function in VB/ASP, and -- I think -- the .IndexOf property in VB.Net.  Something like this:

Position = Sentence.IndexOf(Word)

Position will be a number represented where Word was found in Sentence.  It will return 0 if Word was not found.
0
 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 11957061
Why not just use the .IndexOf function.  It would look like this:

MyString = "This is my test string"
x=MyString.IndexOf("test")

This will return -1 if it isn't there or the actual zero based index if it is.  In this case it would return 11 (I think).

 
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11957572
So putting it all together in a function would look something like this:

    Private Function hasWord(ByVal sentence As String, ByVal word As String) As Boolean
        If sentence.IndexOf(word) > -1 Then
            Return False
        Else
            Return True
        End If
    End Function

Idle_Mind
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 11957618
Logic is backwards

 Private Function hasWord(ByVal sentence As String, ByVal word As String) As Boolean
       If sentence.IndexOf(word) > -1 Then
            Return True
        Else
            Return Flase
        End If
    End Function
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11957732
Good Grief!

...Thank You Joe_Griffith!

Perhaps I should have taken a nap with my two year old this afternoon...

=)
Idle_Mind
0
 
LVL 7

Expert Comment

by:Joe_Griffith
ID: 11957791
Naps are always good.  Would probably have helped me to spell False correctly.
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 11961734
Private Function Button1_Click(ByVal string1 As String, ByVal string2 As String) As Boolean
        If Microsoft.VisualBasic.InStr(string1, string2) = 0 Then
            Return False
        Else
            Return True
        End If
    End Function

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now