|
-
Jan 23rd, 2002, 07:14 AM
#1
Thread Starter
Hyperactive Member
easy code?
This should prove no problem, but my VB is very rusty.
Say the user enters a string of 12 digit numbers, it will always be 12 digits. e.g.
235467890318
What is the code to loop through this string, and retrieve the numbers at positions 6 7 8 9 and 10
(i.e. in this eg the numbers needed are 78903)
Hope that makes sense, many thanks
Steve
-
Jan 23rd, 2002, 07:20 AM
#2
Retired VBF Adm1nistrator
VB Code:
Private Sub printOutFrom(ByVal strString As String, ParamArray atPositions())
Dim i As Long, x As String: x = "," & Join(atPositions, ",") & ","
For i = 1 To Len(strString)
If InStr(x, "," & i & ",") <> 0 Then
Debug.Print "Character at position : " & i & " : " & Mid(strString, i, 1)
End If
Next
End Sub
Private Sub Form_Load()
Dim a As String: a = "235467890318"
printOutFrom a, 6, 7, 8, 9, 10
End Sub
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 23rd, 2002, 07:21 AM
#3
VB Code:
Dim MyNumber As String
Dim Result As String
MyNumber = 235467890318
Result = Mid(MyNumber, 6, 5)
And mid is like this:
Mid(String, Starting position (first is 1), How many characters?)
Hope this helps,
-
Jan 23rd, 2002, 07:22 AM
#4
plenderj: Why make the thing so complicated?
-
Jan 23rd, 2002, 07:29 AM
#5
Retired VBF Adm1nistrator
Originally posted by MerryVIP
plenderj: Why make the thing so complicated?
More robust
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 23rd, 2002, 07:31 AM
#6
But wasn't he asking easy code?
-
Jan 23rd, 2002, 07:36 AM
#7
Thread Starter
Hyperactive Member
yeah
the easy code works great, the other code scares me
Thanks to you both
-
Jan 23rd, 2002, 07:42 AM
#8
-
Jan 23rd, 2002, 08:46 AM
#9
Retired VBF Adm1nistrator
I think my code is ok... doesn't look too scary to me
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 25th, 2002, 06:41 AM
#10
The thing is in the amount of code and if it's easy for a beginner to understand. The less code, the less complex, the easier it is. I think I'm kind of professional in this, keeping things simple and understandable (I have done that all my life, one of my targets always when I've coded a program/game).
Anyways, what were too much in your example:
- new sub, which contains a lot of things, which a beginner probably doesn't know (to mention, declaring strings and ByVal)
- use of for-loop, if, InStr, Debug-window and Mid together really makes it messy to look at except for an experienced coder like you and me (yet, I needed to think it a few times before I understood everything - beginners usually want something to come out fast too)
- the code lines are too long (sometimes this is a "have-to-do" situatation, but not in this case) (it's far easier to read many short lines vertically, seeing fast what a line does, than to read one really long line)
Yay, I wrote a that...people could learn something about it
-
Jan 25th, 2002, 06:45 AM
#11
Retired VBF Adm1nistrator
I think the ParamArray is probably the most extreme thing in there
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 25th, 2002, 06:52 AM
#12
Heh, I missed that one That thing doesn't even exist in VB4...darn, I talk a lot about that, VB4
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
|