Results 1 to 10 of 10

Thread: Do not understand this function stand for???

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    7

    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:
    1. Public Function BreakStr(ByVal Text As String, ByVal Char As String, ByVal Where As Integer) As String 'Very useful function
    2. Dim i As Long, temp As String, m As Long
    3.  
    4. For i = 1 To Len(Text)
    5.     temp = Mid(Text, i, Len(Char))
    6.         If temp = Char Then
    7.             m = i
    8.             If Where = 1 Then 'before
    9.                     temp = Mid(Text, 1, m - 1)
    10.                     BreakStr = temp
    11.                     Exit Function
    12.             Else
    13.                     temp = Mid(Text, m + Len(Char), Len(Text))
    14.                     BreakStr = temp
    15.                     Exit Function
    16.             End If
    17.         End If
    18. Next
    19. If Where = 1 Then BreakStr = Text
    20. End Function
    Last edited by MartinLiss; Apr 29th, 2006 at 02:11 PM.

  2. #2
    Addicted Member o0yuna0o's Avatar
    Join Date
    Mar 2006
    Posts
    172

    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

  3. #3
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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:
    1. 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.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    7

    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:
    1. 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?

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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

  6. #6

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    7

    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.

  8. #8
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    7

    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>
    Last edited by newhondacivic; Apr 30th, 2006 at 11:01 AM.

  10. #10

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width