Posted 18 Mar 2020 Hi, Could anyone help me out on how to extract the digits present in a string in sysmac studio... Thanks in advance !!! Share this post Link to post Share on other sites
Posted 18 Mar 2020 By digits, do you mean characters? Share this post Link to post Share on other sites
Posted 18 Mar 2020 I meant extracting numbers from the string Share this post Link to post Share on other sites
Posted 18 Mar 2020 Numbers? A string is made up of ASCII characters. If your string was: 'ABC12345' Do you want to extract the number 3 (for example) from the string, or do you want to know the HEX number for that character, which is 33 Hex? Share this post Link to post Share on other sites
Posted 18 Mar 2020 I want to extract 12345 in to another variable. If the string is ABC123CED , we want to extract 123 from that Share this post Link to post Share on other sites
Posted 18 Mar 2020 I don't think that i am quite understanding what you are trying to do, if you want to extract 123 from a string, the answer is just 123. I have used your example however to provide code that will search one string for 123 and then extract 123 from the string and store it in a variable. The last rung will check to make sure that the string returned matches the string that was searched for, confirming that the string was indeed found. If nothing else, you will see how these instructions function and they can help you to solve your problem. Share this post Link to post Share on other sites
Posted 19 Mar 2020 HI Michael, thanks for the support ! I want the numbers in a string to be extracted to another variable. e.g.1. M1e2s3h4 is the string now 1234 should be extracted to another variable. e.g.2. M123esh -> 123 e.g.3. Mes12h4 -> 124 Share this post Link to post Share on other sites
Posted 19 Mar 2020 (edited) Your request keeps changing. So it looks like your string is variable. Is that correct? Where is this string coming from? Is this an external device? Do you have the manual? Are there rules for the string format? For example: Do you want to extract numbers only and discard all letters? If so, will this rule apply to all possibilities? Edited 19 Mar 2020 by IO_Rack Share this post Link to post Share on other sites
Posted 19 Mar 2020 Right..the string is a variable. The string comes from the NB-HMI. There are no rules for the string format. It could be "174Mesh" or "Mesh174" . I want to extract the numbers and use it for further processing. In my initial request, i mentioned as "digits" instead of "numbers", by mistake. I apologize for that. Share this post Link to post Share on other sites
Posted 19 Mar 2020 Ok, so you want to extract all the numeral characters from the string and list them in order of appearance. Try this in an Inline structured Text block: FoundNumbers:=''; //make the result string blank, or this will keep growing and growing FOR Index:=1 TO 100 BY 1 DO; //This is for a 100 character string FoundChar:='';//makes FoundChar blank FoundChar:=MID(StringToSearch,UINT#1,Index); //extracts one character at a time StringGreater:=GEascii(FoundChar,'0'); //Checks to see if the character is >= 0 StringLessThan:=LEascii(FoundChar,'9'); //Checks to see if the character is <= 9 IF StringGreater AND StringLessThan THEN; //if >=0 and <=9, then it is a number FoundNumbers:=CONCAT(FoundNumbers,FoundChar); //add number to result string END_IF; END_FOR; Here is a picture if it is easier to visualize: Here are the data types of all the variables: Name StringToSearch string[100] False False Index UINT False False FoundChar String[2] False False This technically only has to be a length of 2 (one for the character and one for a null character). StringGreater BOOL False False StringLessThan BOOL False False FoundNumbers STRING[100] False False Share this post Link to post Share on other sites
Posted 19 Mar 2020 Adding this little bit of code in yellow will make the For...Next loop only execute as many times as there are characters in the string to search: Note the index only counted to 7 as there are 6 characters in the string and it had to increment one more time to find the NULL character. Share this post Link to post Share on other sites