none
How to check if a variable contains a specific value? RRS feed

  • Question

  • Hi!

    I'm trying to create a an IF statement to check if a variable contains a certain value, but how not figured out how to do so inn Small Basic.

    This is what i got:

    v1 = "This is a test"
    
    If v1 contains "Test" Then
      TextWindow.WriteLine("Yes, it contains the word 'Test'")  
    Else
      TextWindow.WriteLine("No, it does NOT contain the word 'Test'")
    EndIf

    It obviously won't let me run the program, since it does not recognize 'contains'.

    Any suggestions?
    Thursday, March 4, 2010 8:58 PM

Answers

  • Try the following, the first is an exact match (including case sensitive) and the second is case insensitive:

    v1 = "This is a test"

    If Text.IsSubText(v1,"Test") Then
      TextWindow.WriteLine("Yes, it contains the word 'Test'") 
    Else
      TextWindow.WriteLine("No, it does NOT contain the word 'Test'")
    EndIf

    If Text.IsSubText(Text.ConvertToLowerCase(v1),Text.ConvertToLowerCase("Test")) Then
      TextWindow.WriteLine("Yes, it contains the word 'Test'") 
    Else
      TextWindow.WriteLine("No, it does NOT contain the word 'Test'")
    EndIf
    • Marked as answer by The_face Saturday, March 6, 2010 10:26 AM
    Thursday, March 4, 2010 10:01 PM
    Moderator
  • HMH849 http://smallbasic.com/program/?HMH849

    SourceText = "This is a test"

    Checkfor = "Test"
    TestFor()

    Checkfor = "test"
    TestFor()

    Sub TestFor
      FoundBeginingAt = Text.GetIndexOf(SourceText,Checkfor) 
      TextWindow.WriteLine("Position begining at = " + FoundBeginingAt)
      If FoundBeginingAt <> 0 Then
        TextWindow.WriteLine("Yes, it contains the word " + Checkfor) 
      Else
        TextWindow.WriteLine("No, it does NOT contain the word " + Checkfor)
      EndIf
    EndSub

    The thing to note in this example is that Text.Getindexof is case sensitive.

    • Edited by Robert W Jones Thursday, March 4, 2010 11:38 PM typo
    • Marked as answer by The_face Saturday, March 6, 2010 10:26 AM
    Thursday, March 4, 2010 11:38 PM

All replies

  • Try the following, the first is an exact match (including case sensitive) and the second is case insensitive:

    v1 = "This is a test"

    If Text.IsSubText(v1,"Test") Then
      TextWindow.WriteLine("Yes, it contains the word 'Test'") 
    Else
      TextWindow.WriteLine("No, it does NOT contain the word 'Test'")
    EndIf

    If Text.IsSubText(Text.ConvertToLowerCase(v1),Text.ConvertToLowerCase("Test")) Then
      TextWindow.WriteLine("Yes, it contains the word 'Test'") 
    Else
      TextWindow.WriteLine("No, it does NOT contain the word 'Test'")
    EndIf
    • Marked as answer by The_face Saturday, March 6, 2010 10:26 AM
    Thursday, March 4, 2010 10:01 PM
    Moderator
  • HMH849 http://smallbasic.com/program/?HMH849

    SourceText = "This is a test"

    Checkfor = "Test"
    TestFor()

    Checkfor = "test"
    TestFor()

    Sub TestFor
      FoundBeginingAt = Text.GetIndexOf(SourceText,Checkfor) 
      TextWindow.WriteLine("Position begining at = " + FoundBeginingAt)
      If FoundBeginingAt <> 0 Then
        TextWindow.WriteLine("Yes, it contains the word " + Checkfor) 
      Else
        TextWindow.WriteLine("No, it does NOT contain the word " + Checkfor)
      EndIf
    EndSub

    The thing to note in this example is that Text.Getindexof is case sensitive.

    • Edited by Robert W Jones Thursday, March 4, 2010 11:38 PM typo
    • Marked as answer by The_face Saturday, March 6, 2010 10:26 AM
    Thursday, March 4, 2010 11:38 PM