Re: find word(s) in string
From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 06/29/04
- Next message: SStory: "Re: screenshot in GDI...."
- Previous message: Dave Cullen: "Re: file access with VB.NET"
- In reply to: BrianDH: "find word(s) in string"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 29 Jun 2004 13:36:37 -0500
Brian,
I would consider using a regular expression.
Something like:
Const input As String = "One Two ICE Three"
Const input2 As String = "One NC Two Three"
Const input3 As String = "NC is the way"
Const input4 As String = "The train is an ICE"
Dim re As New
System.Text.RegularExpressions.Regex("(^|\s+)(ICE|NC)($|\s+)",
RegularExpressions.RegexOptions.Compiled)
Debug.WriteLine(re.IsMatch(input), input)
Debug.WriteLine(re.IsMatch(input2), input2)
Debug.WriteLine(re.IsMatch(input3), input3)
Debug.WriteLine(re.IsMatch(input4), input4)
If you need to know if you matched either ICE or NC, you can use a named
group, something like:
Dim re As New
System.Text.RegularExpressions.Regex("(^|\s+)(?'tag'ICE|NC)($|\s+)",
RegularExpressions.RegexOptions.Compiled)
Matchedwhat(re, input)
Matchedwhat(re, input2)
Matchedwhat(re, input3)
Matchedwhat(re, input4)
Private Shared Sub MatchedWhat(ByVal re As
System.Text.RegularExpressions.Regex, ByVal input As String)
Dim m As System.Text.RegularExpressions.Match = re.Match(input)
If m.Success Then
Debug.WriteLine(m.Groups("tag"), "matched")
Else
Debug.WriteLine(False, "failed")
End If
End Sub
FWIW: The regular expression I gave is:
(^|\s+) = match either the beginning of the line or one or more whitespace
characters
(ICE|NC) = match either ICE or NC
($|\s+) = match either the ending of the line or one or more whitespace
characters.
Hope this helps
Jay
"BrianDH" <BrianDH@discussions.microsoft.com> wrote in message
news:184B4117-6300-42A2-B992-2C1FDE738E93@microsoft.com...
> I am trying to find "NC" or "ICE" in a string that will vary in length
> Example:
>
> String = "One Two ICE Three"
> Or
> String = "One NC Two Three"
>
> I need to know if "ICE" or "NC" is within the text string.
>
> I have tried "InStr" but it shows true if there is a word that hasthe
letters "NC" withina word.
>
> Any Help
>
> Thank you
>
- Next message: SStory: "Re: screenshot in GDI...."
- Previous message: Dave Cullen: "Re: file access with VB.NET"
- In reply to: BrianDH: "find word(s) in string"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|