Solved

VB.NET string function

Posted on 2004-09-01
7
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

735 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