string.IndexOf()

finding a word in a string

Page 1 of 1

5 Replies - 4401 Views - Last Post: 05 December 2009 - 05:51 PM Rate Topic: -----

#1 deery5000   User is offline

  • D.I.C Lover

Reputation: 87
  • View blog
  • Posts: 1,097
  • Joined: 09-May 09

string.IndexOf()

Post icon  Posted 05 December 2009 - 02:04 PM

Hi guys :)

im trying to search for a word in a string, when i find that word i want to enter into my if statement based upon
dim position = 1

im aware that postion is 0 by default and -1 when the word is not there and 1 when its there.

'find covers and back drop 
	Sub findfiles()

		Dim file_names As ReadOnlyCollection(Of String)
		Dim directory As String = "B:\Movies"

'returns node name of selected item in treeView 
		Dim movieName = movieTreeView.SelectedNode.ToString()
'eg treeNode: Transformers 2

'spliting movieName into 2 words
		Dim words As String() = Nothing
		words = movieName.Split(":")

		Dim i As Integer = 0
		While (1)

			' Dim word As String

'think the problem is here
'stepping through file paths ans searching for transformers 2 in string 
'words(1) gives me 2nd part of split, the movie name
			Dim position = filePath(i).IndexOf(words(1))

'if word is found in string, go and search directory  for cover 
			If position = 1 Then

				file_names = My.Computer.FileSystem.FindInFiles(filePath(i), "folder-front", True, _
				FileIO.SearchOption.SearchAllSubDirectories)

				For Each file_name As String In file_names
					frontPictureBox.Image = Image.FromFile(directory)
				Next file_name
			Else

				i = i + 1
			End If

		End While

	  
	End Sub



i get alist of directories and store them in an array. I step through the array (i) while search the string for the correct word.

Hope you can help
Kevin Deery

This post has been edited by deery5000: 05 December 2009 - 03:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: string.IndexOf()

#2 sharpy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 396
  • Joined: 02-June 07

Re: string.IndexOf()

Posted 05 December 2009 - 03:43 PM

Hi Kevin

Have you tried

 If Word.Contains("Whatever") Then
		   
' Do something
 

End If


The whatever could be your variable

Just a suggestion

This post has been edited by sharpy: 05 December 2009 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 June7   User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: string.IndexOf()

Posted 05 December 2009 - 03:43 PM

Why does code not work? What are error messages? Runs but bad or no results? Provide sample data.
Was This Post Helpful? 0
  • +
  • -

#4 deery5000   User is offline

  • D.I.C Lover

Reputation: 87
  • View blog
  • Posts: 1,097
  • Joined: 09-May 09

Re: string.IndexOf()

Posted 05 December 2009 - 04:41 PM

Hi mate the code runs fine and i have debugged that sucker lots of times using step over to see the values of my variables.

the problem is the IndexOf

this line
Dim position = filePath(i).IndexOf(words(1))



position = 0 (default) -1 for not finding the word and 1 if word is present.

i never make it to 1, always -1 even when the word isnt there.

filePath(i) = B:\Movies\Transformers 2
IndexOf(word(1)) = Transformers 2

so when word is present i believe the value of postion will change to 1.

i then try
if postion = 1 then
do something

Is this clear now?

Kevin

oh and mate ill try that method of yours out too

This post has been edited by deery5000: 05 December 2009 - 04:43 PM

Was This Post Helpful? 0
  • +
  • -

#5 deery5000   User is offline

  • D.I.C Lover

Reputation: 87
  • View blog
  • Posts: 1,097
  • Joined: 09-May 09

Re: string.IndexOf()

Posted 05 December 2009 - 04:52 PM

this not working either for me

Dim movieName = movieTreeView.SelectedNode.ToString()

		Dim word As String() = Nothing
		word = movieName.Split(":")

		Dim i As Integer = 0
		While (1)

			' Dim word As String

			Dim path = filePath(i)

			If path.Contains(word(1)) Then

				file_names = My.Computer.FileSystem.FindInFiles(filePath(i), "folder-front", True, _
				FileIO.SearchOption.SearchAllSubDirectories)

				For Each file_name As String In file_names
					frontPictureBox.Image = Image.FromFile(directory)
				Next file_name
			Else

				i = i + 1
			End If

		End While


Was This Post Helpful? 0
  • +
  • -

#6 deery5000   User is offline

  • D.I.C Lover

Reputation: 87
  • View blog
  • Posts: 1,097
  • Joined: 09-May 09

Re: string.IndexOf()

Posted 05 December 2009 - 05:51 PM

SOLVED

Use the string.contains method
when i had split the strings at : there was a white space
i failed to notice this in the debugger.

Idiot
Kevin
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1