Do not understand this function stand for???
i'm newbie in VB6. Can somebody explain what is this function for? I get this function from internet and for learning knowledge. Thank you.
VB Code:
Public Function BreakStr(ByVal Text As String, ByVal Char As String, ByVal Where As Integer) As String 'Very useful function
Dim i As Long, temp As String, m As Long
For i = 1 To Len(Text)
temp = Mid(Text, i, Len(Char))
If temp = Char Then
m = i
If Where = 1 Then 'before
temp = Mid(Text, 1, m - 1)
BreakStr = temp
Exit Function
Else
temp = Mid(Text, m + Len(Char), Len(Text))
BreakStr = temp
Exit Function
End If
End If
Next
If Where = 1 Then BreakStr = Text
End Function
Re: Do not understand this function stand for???
hav u tried to test it... you can also make your own function
im sorry i cant understand this code bcoz it seems have errors [somebody correct me if im wrong]
the condition if WHERE = 1 - i think this will produce syntax error
Re: Do not understand this function stand for???
As the name suggests the function is used to break string at a specified character position.
for example, if you write
VB Code:
Debug.Print BreakStr("this is a test", "s", 0)
Then the result will be is a test. What the function will do is that it will take all the characters after the first s and print return them. Now if you write anyother value apart from 0 then it will take all the characters that are to the left of the first occurence of the character.
This can be achieved by using the built-in vb functions like Mid, Instr & Left. There is no need to write the function.
Re: Do not understand this function stand for???
Quote:
Originally Posted by Shuja Ali
As the name suggests the function is used to break string at a specified character position.
for example, if you write
VB Code:
Debug.Print BreakStr("this is a test", "s", 0)
Then the result will be
is a test. What the function will do is that it will take all the characters after the first
s and print return them. Now if you write anyother value apart from 0 then it will take all the characters that are to the left of the first occurence of the character.
This can be achieved by using the built-in vb functions like Mid, Instr & Left. There is no need to write the function.
temp = Mid(Text, i, Len(Char))
If temp = Char Then
m = i
Why we need to check the length of character?
Re: Do not understand this function stand for???
it's not checking the length, it's returning a string the same length as Char (in case Char is longer than one - it's a bit misnamed)
This function should be using Split, and can be done a lot easier with it
Re: Do not understand this function stand for???
I added vbcode tags to your post to make it easier to read.
Re: Do not understand this function stand for???
Quote:
Originally Posted by bushmobile
it's not checking the length, it's returning a string the same length as Char (in case Char is longer than one - it's a bit misnamed)
This function should be using Split, and can be done a lot easier with it
Ok..thank you friend.
Do you have any example to show how this code works?
Example: string => "I am a boy"
How it runs?
Any idea. Shariing is caring. ;)
Re: Do not understand this function stand for???
I would suggest, try to execute it yourself. It wil be easier to understand when you debug through the function.
Re: Do not understand this function stand for???
Between, is it this same like the origin example??
if no then what is the difference? Thank you.
<vb code>
Public Function BreakStrEx(ByVal Text As String, ByVal Char As String, ByVal Where As Integer) As String
Dim i As Long, temp As String, m As Long
For i = Len(Text) To 1 Step -1
temp = Mid(Text, i, Len(Char))
If temp = Char Then
m = i
If Where = 1 Then 'before
temp = Mid(Text, 1, m - Len(Char))
BreakStrEx = temp
Exit Function
Else
temp = Mid(Text, m + Len(Char), Len(Text))
BreakStrEx = temp
Exit Function
End If
End If
Next
If Where = 0 Then BreakStrEx = Text
End Function
<vb code>
Re: Do not understand this function stand for???
You almost got the vbcode tags right, the one at the end should be [/vbcode]