|
-
Jul 8th, 2008, 06:51 AM
#1
Thread Starter
New Member
[2005] Howto search through a string
I am trying to search for a certain string inside another string. Thanks alot for any help
-
Jul 8th, 2008, 06:58 AM
#2
Re: [2005] Howto search through a string
you can use [string].Contains
vb Code:
Dim string1 As String = "some text"
MsgBox(string1.Contains("text"))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 8th, 2008, 07:39 AM
#3
Re: [2005] Howto search through a string
You can also use the IndexOf method or Regular Expressions. If you just purely need to see if a string contains another string then Paul's method will work fine but if you need to find that string (within the original string) and then manipulate it or get its position etc then the IndexOf or RegEx methods may be better.
-
Jul 9th, 2008, 01:54 AM
#4
Thread Starter
New Member
Re: [2005] Howto search through a string
What's the IndexOf method?
-
Jul 9th, 2008, 01:59 AM
#5
Re: [2005] Howto search through a string
From MSDN:
Reports the index of the first occurrence of the specified Unicode character in this string.
Usage:
Code:
Dim myStringVariable as string = "Hello World"
Messagebox.Show(myStringVariable.IndexOf("World").ToString())
-
Jul 9th, 2008, 02:17 AM
#6
Re: [2005] Howto search through a string
The string class exposes a lot of different methods to help with this:
Code:
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim myStringVariable As String = "Hello World"
' Using StartsWith method:
MessageBox.Show("String starts with word 'Hello': " & _
myStringVariable.StartsWith("Hello"))
' Using IndexOf method:
MessageBox.Show("Position of word 'World' " & _
"in the string is: " & _
myStringVariable.IndexOf("World"))
' Using LastIndexOf method:
MessageBox.Show("Last Position of word 'World' " & _
"in the string is: " & _
myStringVariable.LastIndexOf("World"))
' Using Contains method:
MessageBox.Show("String contains word 'World': " & _
myStringVariable.Contains("World").ToString)
' Using Regular Expressions:
Dim worldSearchregex As New _
System.Text.RegularExpressions.Regex("([.|\n]*)World([.|\n]*)")
MessageBox.Show("String includes 'World' expression: " & _
worldSearchregex.Match(myStringVariable).Success.ToString)
' Using Equals method:
MessageBox.Show("String equals text 'Hello World': " & _
myStringVariable.Equals("Hello World").ToString)
End Sub
-
Jul 9th, 2008, 03:20 AM
#7
Thread Starter
New Member
Re: [2005] Howto search through a string
This is very helpful, Thanks alot.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|