Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.NET string function

Posted on 2004-09-01
7
Medium Priority
?
468 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 320 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 86

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 86

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

661 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