•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 401,718 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,092 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 2173 | Replies: 2
![]() |
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 107
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.
•
•
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation:
Rep Power: 7
Solved Threads: 107
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:
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.
Public Function str2Array(xString As String) As String()
Dim tmpArray() As String
Dim B() As Byte
Dim tmpchar As String
B() = xString
For I = 0 To UBound(B) Step 2
spush tmpArray, Chr(B(I))
Next I
str2Array = tmpArray
End Function![]() |
•
•
•
•
•
•
•
•
DaniWeb Visual Basic 4 / 5 / 6 Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Using Mid function and/or statement in IF (Visual Basic 4 / 5 / 6)
- mid problem in access 2003 (Windows NT / 2000 / XP / 2003)
- Method help that calculates a function (Java)
- an "(" expected?? (C++)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: How do I program a multi-menu application?
- Next Thread: mouse over



Linear Mode