writing my own MID FUNCTION

Reply

Join Date: Mar 2006
Posts: 14
Reputation: softwarecaz is an unknown quantity at this point 
Solved Threads: 0
softwarecaz softwarecaz is offline Offline
Newbie Poster

writing my own MID FUNCTION

 
0
  #1
Mar 29th, 2006
Hello I need to write my own mid function, have do I do that, just someone know, I know the mid function definition is mid(String, Start As Long, [Length]), if I wanted to create my own function, how do I do that, does someone have an example
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: writing my own MID FUNCTION

 
0
  #2
Mar 29th, 2006
There are a couple of ways to tackle this problem. I would personally make a function that splits the string into an array, and then grab the passed number as that array's indice. I have attached a project that has 3 functions in it. The first I'll go over is str2Array, which takes each character in a string, and puts it into an array, 1 character per indice. The str2Array function requires the use of another function, spush (String Push), which just sticks elements onto an array, like a stack. It simplifies having to deal with dynamic arrays by rediming every time you want to add a new element to the array. The last is the actual mymid function, which takes 2 arguments (the string, and the starting point), and an optional 3rd argument, which is the length to return to the calling procedure. Something to note about this function, is that it too, just like the actual Mid function, returns the entire rest of the string if the length passed to it is greater than the end of the string. So, for example, if the string is "hello world", and you tell the function to start at character 6, and go a length 15, it will just return world, and not error out. A serious difference, though, (which can be fixed fairly easily) is that the actual Mid function reads the first character of a string as 1, while the mymid function starts reading strings at 0. You can change this easily enough by using an option base, and making a minor change to the if statement in the mymid function, but I'll leave those for you to play with.
Attached Files
File Type: zip MyMid.zip (2.0 KB, 19 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: writing my own MID FUNCTION

 
0
  #3
Mar 29th, 2006
oops, fixing it, standby (I just realized that str2Array uses Mid, that's kind of a conflict of idea's, huh?)... just replace the old str2array with this one:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Function str2Array(xString As String) As String()
  2. Dim tmpArray() As String
  3. Dim B() As Byte
  4. Dim tmpchar As String
  5.  
  6. B() = xString
  7. For I = 0 To UBound(B) Step 2
  8. spush tmpArray, Chr(B(I))
  9. Next I
  10.  
  11. str2Array = tmpArray
  12. End Function
Basically, it assigns the string to a byte array. The byte array contains numbers instead of letters, but the numbers directly coorispond to the letters, so we use chr with the character code to get the letter. We push that onto our array, and then return it to the calling procedure. This method is actually a lot faster than using the mid function (with larger amounts of data), because it doesn't have to make a bunch of copies of the string, AND it's working with bytes.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC