The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Extracting a number and substring from a string

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
CStWue Posted - 11/20/2018 : 08:26:21 AM
Origin 2019 (64-bit)
Windows 10

Hi,

I have a worksheet with several columns where the second user parameter looks something like this:
col1: 0.5µM
col2: 1µM
...
col(20): 10mM

I need to extract two parts from each of these. 1) The numeric value as a double, 2) the letters as a string. The trouble is that the numbers all have different lengths and the letters also change between columns. Also, there is no space between the two.

Right now I'm using the following approach:

for(int kk = 1; kk <= wks.ncols; kk++)
{
  cAdd$ = col($(kk))[D2]$;
  cAddLength = cAdd.GetLength();
  cAdd.Delete(cAddLength);
  cAdd.Delete(cAddLength-1);
  cAddVal = cAdd.Value();
  cAddUnit$ = col($(kk))[D2]$;
  cAddUnit.Delete(count=cAddLength-2);
}


Of course, this only works when there are 2 letters in the user parameter. How could I generalize the method?

Thanks!
1   L A T E S T    R E P L I E S    (Newest First)
nick_n Posted - 11/20/2018 : 2:38:15 PM
Hi,

This script will find index of last digit in the string and insert space. Thus, further you can use GetToken.

int lastDigIndex=0;
string cAdd$="0.55mmol/L";

for (ii=cAdd.Len(); ii>0; ii--)
{
	int ch = cAdd.GetAt(ii);
	if (ch>47 && ch<58) {lastDigIndex=ii; break;}
}
cAdd.Insert(lastDigIndex+1, " ");
cAdd$=;

0.55 mmol/L


Nikolay

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000