Solved

VB.NET string function

Posted on 2004-09-01
7
456 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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