Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    133
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Checking if a word or a group of words exist in a variable

    Hi all,

    I would like to learn if there is a function in ASP checking if a word or group of words included in a variable???

    Example:

    test="This is an example sentence"

    I am looking for a function logic like:

    if "example sentence" is in test then

    Thanks

    Ceyhun

  • #2
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <%
    words = Array("red", "bat", "ball")
    sentence = "Johnny is kicking a ball that is red"
    
    	For x=0 To Ubound(words)
    		If InStr(sentence, words(x)) Then
    			result = result & words(x) & ","
    			sentence = Replace(sentence, words(x), "<strong>" & words(x) & "</strong>")
    		End If
    	Next
    
    	If Len(result) > 0 Then
    		Response.Write "The sentence<br>""" & sentence & """<br>contains " & Left(result, Len(result)-1)
    	Else
    		Response.Write "words not found in sentence"
    	End If
    %>
    Last edited by degsy; 04-18-2006 at 03:30 PM.

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    133
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks Degsy. Actually by saying "group of words" I meant exactly a sentence. For example:
    "This is an example sentence,This is the second example sentence" is my variable value.
    I want to check if exactly the "This is the second example sentence" exists in the variable value. I don't want the code catch words example or sentence and say that it exists twice in the long text.

    I don't know if I could explain what I try to tell. Sorry

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try this:

    PHP Code:
    MyString "blah blah blah blah This is the second example sentence blah blah blah blah"
    StringImLookingFor "This is the second example sentence"

    If InStr(MyString,StringImLookingFor ) > 0 Then
        response
    .write "String Exists"
    Else
        
    response.write "String not found"
    End If 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •