vb.net - finding a substring in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zion99
    New Member
    • Jul 2007
    • 2

    vb.net - finding a substring in a string

    Hi,
    I m currently working in VB .NET 2003.
    I have the following string:
    sBIG = "A final Goodbye from Greats"
    I need to find whether the above word contains any word say "from".

    I could not find a method in VB.net that can provide me the result.
    Thanks,
  • TRScheel
    Recognized Expert Contributor
    • Apr 2007
    • 638

    #2
    Originally posted by zion99
    Hi,
    I m currently working in VB .NET 2003.
    I have the following string:
    sBIG = "A final Goodbye from Greats"
    I need to find whether the above word contains any word say "from".

    I could not find a method in VB.net that can provide me the result.
    Thanks,
    I assume you want to check each word?

    If so...

    [code=vbnet]
    For i As Integer = 0 To sBig.Split(" ").Length - 1 Step 1
    If (sBig.Split(" ")(i).ToLower() .Contains("from ")) Then
    Console.WriteLi ne("Word #{0} '{1}' Contains 'from'", i + 1, sBig.Split(" ")(i))
    End If
    Next
    [/code]

    Comment

    • programmerboy
      New Member
      • Jul 2007
      • 84

      #3
      Originally posted by TRScheel
      I assume you want to check each word?

      If so...

      [code=vbnet]
      For i As Integer = 0 To sBig.Split(" ").Length - 1 Step 1
      If (sBig.Split(" ")(i).ToLower() .Contains("from ")) Then
      Console.WriteLi ne("Word #{0} '{1}' Contains 'from'", i + 1, sBig.Split(" ")(i))
      End If
      Next
      [/code]
      I think he just wants to check word "from". You can just say

      [CODE=vbnet]if (index = sBig.indexOf("f rom")) <> -1 then
      //Do whatever you want to do
      end if[/CODE]
      indexOf returns -1 if the specified string is not found.

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        Originally posted by programmerboy
        I think he just wants to check word "from". You can just say

        [CODE=vbnet]if (index = sBig.indexOf("f rom")) <> -1 then
        //Do whatever you want to do
        end if[/CODE]
        indexOf returns -1 if the specified string is not found.
        Rereading his question, you may be right

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          If you don't care where in the string the word is, just if it exists or not you could use:
          Code:
          if (aBig.Contains("From"))
          {//the word "From" appears in aBig
          }

          Comment

          • zion99
            New Member
            • Jul 2007
            • 2

            #6
            Thanks a lot for ur replies.
            But I m afraid, i cud not find CONTAINS for STRING, it exist for STRINGCOLLECTIO N... :(

            so i will be using the INDEX method. but even this method tells if there is an occurrence or not.

            what if i want to know the TOTAL no. of occurrences of the word "FROM" in the statement ?
            Thanks :)
            Last edited by zion99; Jul 11 '07, 10:42 AM. Reason: Adding some more statement.

            Comment

            • TRScheel
              Recognized Expert Contributor
              • Apr 2007
              • 638

              #7
              Originally posted by zion99
              Thanks a lot for ur replies.
              But I m afraid, i cud not find CONTAINS for STRING, it exist for STRINGCOLLECTIO N... :(

              so i will be using the INDEX method. but even this method tells if there is an occurrence or not.

              what if i want to know the TOTAL no. of occurrences of the word "FROM" in the statement ?
              Thanks :)
              [code=cpp]
              while(myString. IndexOf("from") != -1)
              {
              myString = myString.SubStr ing(myString.In dexOf("from"), myString.Length - myString.IndexO f("from"));
              countOfFrom++;
              }
              [/code]

              I didnt test this code, but it should work. If it gives you an array exception for going out of bounds, add a -1 to the final parameter on SubString

              Comment

              • OliveOyl3471
                New Member
                • Jul 2007
                • 8

                #8
                Originally posted by zion99
                Hi,
                I m currently working in VB .NET 2003.
                I have the following string:
                sBIG = "A final Goodbye from Greats"
                I need to find whether the above word contains any word say "from".

                I could not find a method in VB.net that can provide me the result.
                Thanks,

                Here's one way that will work
                Code:
                        Dim sBIG As String = "A final Goodbye from Greats"
                        Dim intIndex As Integer
                
                        intIndex = sBIG.IndexOf("from", 0)
                
                        If intIndex <> -1 Then
                            lblDisplay.Text = "Your word is contained in this string"
                
                        End If
                this code will search for whatever word you put in the parentheses after IndexOf.
                If the compiler doesn't find your word in the string, then it will assign a value of -1 to the variable intIndex.

                It is case sensitive so you could convert it to upper if you need to search for a word that might or might not contain capital letters.

                Now, do you know how to populate a two dimensional array from a text file?

                p.s. I didn't see that others have already answered your question.
                Last edited by OliveOyl3471; Jul 12 '07, 05:01 AM. Reason: to add a p.s.

                Comment

                Working...