Solved

Left - Right - Mid Functions

Posted on 2000-02-10
Medium Priority
255 Views
I am having a lot of trouble
understanding the Left - Right and Mid functions and how they work. If i gave you an example of one could you break it down and explain to me what each piece of code is doing?

Lets say that FirstString = < and SecondString = > Lets say TheWholeString = <L>

We will use the Mid Function As the example.

Text1.Text = Mid\$(TheWholeString,_ InStr(TheWholeString,_
FirstString) + Len(FirstString),_ InStr(TheWholeString,_
SecondString) - (Len(FirstString) +_
InStr(TheWholeString, FirstString)))

I know after running this code, text1.text would = L
but i cant figure out how it does this. Can you explain
to me what each piece of code in the brackets is doing.
0
Question by:poorboy
• 2

LVL 1

Expert Comment

ID: 2509606
Where did you come up with this?  Okay, let's break it down.  You have the Mid function with parameters a, b, c. thus Mid(a, b, c).

a is the string named TheWholeString or <L>

b is InStr(TheWholeString, FirstString) + Len(FirstString)  OR

InStr(<L>, <) equals 1 since the character first appears in the first position of the target string <L>.  If the target string had been &&<<<L>> then the value of InStr(<L>, <) would be 3.

Len(<) equals 1 or the length of the target string.

Therefore b = 1 + 1 = 2

c is InStr(TheWholeString, SecondString) - (Len(FirstString) +                  InStr(TheWholeString, FirstString)))

InStr(<L>, >) = 3

Len(<) = 1

InStr(<L>, <) = 1

So c = 3 - (1 + 1) = 1

Now back to our original formula

Mid(<L>, 2, 1) means start at position number 2 in the string <L> and take 1 charachter.  Thus the answer is L.

More examples:   Mid("Hello", 2, 3) = ell

Mid("Good Morning", 3, 6) = od Mor

InStr("Hello", l) = 3

InStr("Hello", o) = 5

Len("Good Morning") = 12

Hope this helps.

0

LVL 14

Accepted Solution

mcrider earned 400 total points
ID: 2509663
You need to look at the function in  algebraic terms... Always resolve things in parenthesis to get to the lowest absolute, which in this case is the Mid\$ function:

Mid\$ returns a substring of a string.  There are 3 arguments that are passed to Mid\$:

mid\$(String_to_use, Start_of_substring, Length_of_substring)

Here is an algebraic breakdown of the formula. Paste the following into VB so it will make sense.  It will not show up properly here...

Dim FirstString, SecondString, TheWholeString
FirstString = "<"
SecondString = ">"
TheWholeString = "<L>"

text1.Text = Mid\$(TheWholeString, InStr(TheWholeString, FirstString) + Len(FirstString), InStr(TheWholeString, SecondString) - (Len(FirstString) + InStr(TheWholeString, FirstString)))
'                                 \                                /   \              /  \                                 /    \              /   \                               /
'                                  \           returns            /     \   returns  /    \            returns            /      \   returns  /     \           returns           /
'                                                 1                  +         1       ,                  3                  -  (       1        +                 1                 )
'
'  Plug the numbers into the mid\$ function, so it looks like this:
'
'       text1.Text = Mid\$(TheWholeString, 1 + 1, 3 - (1 + 1))
'
'
'  Resolve the numbers again so it looks like this:
'
'       text1.Text = Mid\$(TheWholeString, 2, 3 - 2)
'
'
'  Resolve the numbers again so it looks like this:
'
'       text1.Text = Mid\$(TheWholeString, 2, 1)

Hope this helps!

Cheers!
0

Author Comment

ID: 2509765
I just made the formula up in case
you were wondering. Thanks for the help.
0

LVL 14

Expert Comment

ID: 2510128
You made the formula up?? Ok...

Thanks for the points! Glad I could help!

Cheers!
0

Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.