functionpowerbuilder

Extract char/numbers using Mid and Pos function


I have a string with a value of "String_test|123456"

How can I extract the numbers and characters and put them to another variable string2 = "String_test" and int = 123456 using Mid / Pos functions.

Thanks!


Solution

  • Assuming you want a PowerBuilder answer rather than the ones given... You seem to have a string with a 'string' portion and a 'number' portion delimited by a pipe character '|'. Assuming this is the normal format of the data you find the position of the pipe by:

    li_pipepos = Pos(ls_string, '|')
    

    Then the string portion is found thusly:

    ls_string_portion = Mid(ls_string, 1, li_pipepos - 1)
    

    The number portion is found:

    ls_number_portion = Mid(ls_string, li_pipepos + 1 )
    

    Then you convert the number portion into an integer (watch out since in PB an integer is not very large - i'd use a long instead) by:

    ll_number = Long(ls_number_portion)
    

    Now if your data isn't in a standardized format you will need to loop through all the characters to determine if they are a number or not and then append them to a string variable (one for numbers and another for characters) then finally convert the number string into a number.