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
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Extracting a number and substring from a string
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

CStWue

5 Posts

Posted - 11/20/2018 :  08:26:21 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
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!

Edited by - CStWue on 11/20/2018 08:29:18 AM

nick_n

Finland
125 Posts

Posted - 11/20/2018 :  2:38:15 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000