Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Networks on June 23, 2004, 09:57 AM

Title: Parsing letters
Post by: Networks on June 23, 2004, 09:57 AM
What's the best way to parse each letter in a sentence or word? Feanor told me a loop would be best from 0 to len(string) is there any other way?
Title: Re:Parsing letters
Post by: Lenny on June 23, 2004, 10:06 AM
Be more specific, exactly what do you want out of the parsed information?
Title: Re:Parsing letters
Post by: Networks on June 23, 2004, 10:13 AM
I want to see if that letter is inside another string.

BTW: Instr() won't work if I have to multiple letters for what I am doing.
Title: Re:Parsing letters
Post by: Lenny on June 23, 2004, 10:26 AM
Are you trying to find the occurence of a series of letters in a string?
If that case, Instr() will work...I'm still uncertain as what you are trying to do.
Title: Re:Parsing letters
Post by: CrAz3D on June 23, 2004, 10:48 AM
I think an example would help us all figure it out.
Title: Re:Parsing letters
Post by: Eli_1 on June 23, 2004, 12:06 PM
We're obviously having a misunderstanding. Either you're confused because you don't know InStr will return the FIRST occurance of something, regardless of how many there are, or we're confused and don't understand the question.
Title: Re:Parsing letters
Post by: Tuberload on June 23, 2004, 01:06 PM
Quote from: Networks on June 23, 2004, 10:13 AM
I want to see if that letter is inside another string.

BTW: Instr() won't work if I have to multiple letters for what I am doing.

Perform a linear search.

Algorithm:
1) Set a location variable to 0.
2) Loop through the char array (string) until you come accrossed the desired element (char) or the end of the array.
2b) increment the location variable.
3) if location equals the length of the char array, set it to -1
4) return the location variable

This will produce either -1 if the character is not found, or the location of the character.

Example (java):
public static int linearSearch (char[] source, char element)
   {
      int location = 0;
      
      while ((source[location] != element) && (location < source.length))
         location++;
      
      if (location == source.length)
         location = -1;
      
      return location;
   }


Now this will only search for one character at a time, but you could easily modify it to find whole strings.
Title: Re:Parsing letters
Post by: Newby on June 23, 2004, 04:21 PM
Dim I as Integer
For I = 0 to Len(String)
   'Each character is Mid$(String, I, 1)
Next I
Title: Re:Parsing letters
Post by: Stealth on June 23, 2004, 05:17 PM
You will want to start the For loop at 1. Mid() is not zero-based.
Title: Re:Parsing letters
Post by: Networks on June 24, 2004, 09:33 AM
Quote from: Newby on June 23, 2004, 04:21 PM
Dim I as Integer
For I = 0 to Len(String)
   'Each character is Mid$(String, I, 1)
Next I


Is what I used and it works.
Title: Re:Parsing letters
Post by: Eli_1 on June 24, 2004, 12:29 PM
Eeek, then you're using On Error Resume Next - Shame.  :P
Title: Re:Parsing letters
Post by: BinaryzL on June 24, 2004, 06:18 PM
Quote from: Newby on June 23, 2004, 04:21 PM
Dim I as Integer
For I = 0 to Len(String)
   'Each character is Mid$(String, I, 1)
Next I


That code will not work, like stealth said mid() starts from 1. (Plus why would you want on error resume next for a simple for loop????)
Title: Re:Parsing letters
Post by: Newby on June 24, 2004, 06:30 PM
Quote from: Stealth on June 23, 2004, 05:17 PM
You will want to start the For loop at 1. Mid() is not zero-based.
I was deciding on putting 0 or 1. I figured I'd put 0 to be safe =P.