Results 1 to 13 of 13

Thread: Split Function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    318

    Question Split Function

    Hi, I am looking for away to know how to split the following string

    str = "F_UNREALF - F_PMUNREALF + F_MTDREALD"

    I need to split by the '-' and '+' symbols (Also, there will be the '*' and '/')


    Please help with any suggestions


    Thanks

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Split Function

    Do a replace of the - + / and * with another symbol then use split on the string using the new symbol as the delimiter.

  3. #3
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,916

    Re: Split Function

    What do you want the end result to be? Are you trying to perform arithmetic on this string? If you do as randem suggests, you will lose the operators.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Split Function

    Yes, but he can always note where they are before replacing them for the split.

  5. #5
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: Split Function

    VB Code:
    1. Dim str As String
    2. Dim oprtr As String
    3.  
    4. str = "F_UNREALF - F_PMUNREALF + F_MTDREALD"
    5.  
    6. If InStr(str, "+") Then oprtr = "+"
    7. If InStr(str, "*") Then oprtr = "*"
    8. If InStr(str, "/") Then oprtr = "/"
    9.  
    10. str = Replace(str, oprtr, "-")
    11.  
    12. MsgBox str
    is this ok?
    noister
    <advertising link removed by moderator>

  6. #6
    New Member
    Join Date
    May 2006
    Location
    Darmstadt, Germany
    Posts
    6

    Re: Split Function

    You can use the Split function to split the string to an array using + as an delimiter, after that you can use Split again for each string in the result array to split it for the - character etc. The only problem will be to handle the result arrays and prepare them for the next go.

    Another way would be to use InStr and Left/Mid functions until no delimiter is left in your string. The only problem here is that you have four delimiters, so you have to call Instr for each delimiter, then select the minimum of them <>0 (which means this string was not found) to determine the position of the first delimiter and then call Left/Mid with that position to split up the string into resultstr and remainder. This goes into a loop which is finished until all four InStr calls on remainder return 0.

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Split Function

    noielen,

    That will only tell him the last operator found. You should save the location and operand in a collection or array to be looped through to perform the operation if needed.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    318

    Smile Re: Split Function

    Basicly the end result would be the following.

    F_xxxx - F_xxxxx represent a records in a table that refrence a column from another table. So F_UNREALF would be "Column 1" and F_UNREALF would be "Column 2".

    I now have a thrid table which holds calculation so F_xxx / F_xxxx. So I will read the calculation into a tmp string, but the bit I am get stuck on is I need to split this up so that I can get the correct values to calculate.

    i.e F_MTDREAL - F_PMMTDREAL + F_MTDUNREAL =
    1000 - 100 + 200 =

    I know I have proberly just confused you, but the idea is not to hard code any calculations into the program

  9. #9
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,916

    Re: Split Function

    Just a thought.... if your strings are always formatted as:
    operand {space} operator {space} operand {space} etc
    you could split on each space which would leave the operands in the odd-numbered array elements and the operators in the even numbered elements. eg
    VB Code:
    1. Dim arr() As String
    2. str = "F_UNREALF - F_PMUNREALF + F_MTDREALD"
    3. arr = Split(str, " ")
    arr(0) = "F_UNREALF"
    arr(1) = "-"
    arr(2) = "F_PMUNREALF"
    arr(3) = "+"
    etc....
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    318

    Re: Split Function

    Actually there is no space, but its a good idea

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    318

    Re: Split Function

    Ok, 1 more question.

    I now have 3 Strings

    s1 = "100"
    s2 = "-"
    s3 = "10"

    if I say s4 = s1 & s2 & s3 I get "100-10". Is there a function that will calculate the s1,s2,s3 if passed into the function

    Thanks

  12. #12
    New Member
    Join Date
    May 2006
    Location
    Darmstadt, Germany
    Posts
    6

    Re: Split Function

    If you're thinking about something like an Eval(expression) function, I don't think that VB has something like that, but if you know that your operator is always in s2 you can write your own function like this:

    VB Code:
    1. Private Function Eval(s1 As String, s2 As String, s3 As String) As Integer
    2.  
    3.     Select Case s2
    4.     Case "+"
    5.         Eval=Val(s1)+Val(s3)
    6.     Case "-"
    7.         Eval=Val(s1)-Val(s3)
    8.     Case "*"
    9. ...
    10.     End Select
    11. End Function

  13. #13
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Split Function

    Yes, Look into the MS Scripting Library Reference. I believe the expression is Evaluate.

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