1. Tracy Lamb
  2. PowerBuilder
  3. Thursday, 9 February 2023 23:24 PM UTC

Hi All,

I've got a dw with a string column.  I'd like to extract just the numerical part of the string for some calculations.

Examples would be "200 mV" or "2V" ...

TIA,

~~~Tracy

John Fauss Accepted Answer Pending Moderation
  1. Friday, 10 February 2023 02:37 AM UTC
  2. PowerBuilder
  3. # 1

Hi, Tracy -

In addition to Miguel's very good suggestion; If the number is always going to be the leading portion of the string value and will not be negative, Then perhaps executing something like:

Long    ll_number = -1
Integer li_i
String  ls_string = "200 mV"

// Examine decreasing amount of leading chars until/if a 
// non-negative whole number is found.

For li_i = Len(ls_string) To 1 Step -1
   If Not IsNumber(Left(ls_string,li_i)) Then Continue
   If Match(Left(ls_string,li_i),"^[0-9]+$") Then
      ll_number = Long(Left(ls_string,li_i))
      Exit
   End If
Next

will meet your needs.

HTH, John

Comment
  1. Miguel Leeuwe
  2. Friday, 10 February 2023 04:56 AM UTC
Hi John,

Nice and simple, but I think there's something missing in "If Match(Left(ls_string,"^[0-9]+$") Then" ..

I haven't tried it (5 am), but it looks like the "Left(" part needs the "li_i" added a closing bracket?

I need coffee now!

:)

regards
  1. Helpful 1
  1. John Fauss
  2. Friday, 10 February 2023 06:25 AM UTC
You are absolutely correct, Miguel! Thank you for catching that. I have corrected it. That's what happens when I try to knock out some code on the fly without actually coding/testing it. LOL. I appreciate your help!
  1. Helpful 1
  1. Miguel Leeuwe
  2. Friday, 10 February 2023 06:32 AM UTC
yw John, same thing happens to me, even when I'm not "on the fly". Thank you!
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Friday, 10 February 2023 00:10 AM UTC
  2. PowerBuilder
  3. # 2

Hi Tracy,

You can create a loop to go through each and every one single character of your initial string, using the Mid() function and then test that single obtained  character to be a number or not by using the IsNumber() function. If it's a number, then you append it to a new string that you create. At the end of the loop, in your new string that only contains numbers, you can use for example the Long() functtion to convert that to a number. (I don't know if you might have decimals and thousands separators also in the string. In that case it becomes a bit more complicated, since you would have to determin if a dot and or comma is just another character in your string or if it's part of a number). You might also have to include dashes '-' to form part of the number or not ...

Can't do any sample code right now, since not able to connect to powerbuiilder at the moment, but let me know if you need a sample of code and I can do it tomorrow.

regards,

MiguelL

Comment
  1. Miguel Leeuwe
  2. Friday, 10 February 2023 00:12 AM UTC
Maybe you could also do something using the Match() function, but regretfully that pb function is not as 'informative' as the Regex() function of other languages like C#.
  1. Helpful 1
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.