Skip to main content
1-Visitor
July 19, 2013
Question

Extract numbers from string in creo

  • July 19, 2013
  • 3 replies
  • 13416 views

Have parameter that is 914klm5ft12or3467 that is a string

i want to filter numbers and put in a parameter that is REAL like 9145123467

 

Any suggestions?

3 replies

15-Moonstone
July 29, 2013

exactly how does the parameter evaluate?

1-Visitor
July 30, 2013

this parameter evaluates some extracted values from tables "extract(TABLE_1,1,3)", "extract(TABLE_1,3,6)"

in old files case evaluates the modelname parameter (that is a string) and compose with extracts the "NUMBERCAD" parameter that drivers the numbering in pdm

in othe case i have to extract only numbers to acquire gps coordinates or absolute coordinates (that are comma separated values)

all those things are acquired as strings and shall be filtered and turned in to numbers

1-Visitor
July 29, 2013

Are the occurences of numbers in that string random? Is the string's length also random?

17-Peridot
July 30, 2013

I can only see this in an extensive -IF- clause that evaluates each entry in the string as a valid "numeric" string that represents a number and next, determine the length of the full number string... and create variables, again based on an extensive -IF- to determine the value of each string"number".

Something like:

(capture every character as a variable)

(evaluate every variable purging alpha characters)

...

if A=="1"

AN=1

endif

if A=="2"

AN=2

endif

...

(Determine the string length)...

(multiply each variable by 10,100, 1000, 10000, 100000, etc...)

(add all the variable together)

Would I want to write this? NOPE!

We need a simple function that will parce a value from a text string by purging non-numeric characters.

As for the OP's request, I would reverse the requirements by making the string from the values rather than making the value from the string.

1-Visitor
July 30, 2013

NUMBERCAD=rel_model_name

than from this parameter "NUMBERCAD" that is a string (because model_name is a string) try to filter and get only numbers.

in creo relation there in no way to extract FROM a string TO a number, but is only possible to do the opposite with this command "extract(PARAMETER,3,6)" (extract from third value of parameter 3 following values)

also i should reverse the requirements but you can't drive new file name in creo and you can't put filters on creation

1-Visitor
October 29, 2016

I'd base it on something like this:

/*Without comments

digitlist = "0123456789"

value = 0

digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-0,1)-1

value = value+digit_found

digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-1,1)-1

value = value+digit_found*10

digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-2,1)-1

value = value+digit_found*100

digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-3,1)-1

value = value+digit_found*1000

d0=value

This was to clip numbers at the ends of parameters. In your case I would have the logic start with the first one and use

IF digit_found <> 0

value = value*10 + digit_found.

ENDIF

Then wrap the digit_found ... and IF digit_found ... in an

IF len(parameter)> whatever_step

digit_found ...

IF digit_found...

value = ...

ENDIF

ENDIF

Repeat for whatever_step = 1 to whatever the maximum number of characters you expect.

Since it's an integer, ITOS() should work OK if you need a string.

HamsterNL
18-Opal
October 24, 2017

This was exactly what I was looking for. In my case, I wanted to convert the last 3 characters (string) from our part name to a number.

 

I have shortened the code just a little bit more:

 

DIGITS = "0123456789"
TEMP = 0
TEMP = TEMP+(SEARCH(DIGITS,EXTRACT(REL_MODEL_NAME,STRING_LENGTH(REL_MODEL_NAME)-0,1))-1)*1
TEMP = TEMP+(SEARCH(DIGITS,EXTRACT(REL_MODEL_NAME,STRING_LENGTH(REL_MODEL_NAME)-1,1))-1)*10
TEMP = TEMP+(SEARCH(DIGITS,EXTRACT(REL_MODEL_NAME,STRING_LENGTH(REL_MODEL_NAME)-2,1))-1)*100

 

You can extend this for as long as you like.

 

Just one warning, there is no error checking!

23-Emerald IV
October 24, 2017

Keep in mind that you're going to get a negative 1 any time this encounters something other than a number (anything not present in DIGITS.)