Solved

VB.NET string function

Posted on 2004-09-01
7
454 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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