2.6.2 String Processing

Using String Registers

String Registers predate the introduction of string variables (Origin 8) and are used more often in older scripts. They are simple to use but are more difficult to read as compared with string variables and their methods. Also, they are global (session scope) and so are more difficult to control in terms of their values being modified by other parts of the code.

// Concatenate two strings using string registers

%A="Left";
%B="Handed";
%N="%A %B";
%N=          // "Left Handed"
// Extract the file name substring from the longer file path string:
%N="C:\Program Files\Origin 8\Samples\Import\S15-125-03.dat";
for(done=0;done==0; )
{
    %M=%[%N,>'\'];
    if(%[%M]>0) %N = %M;
    else done = 1;
}
%N=;

Using String Variables

String variables, introduced in Origin 8, are generally preferable to string registers as they produce more reliable code. To resolve string variables, %( ) substitution is used. The following example shows multiple ways to extract numbers from a string:

// String variables support many methods
string fname$="S15-125-03.dat";

int nn=fname.Find('S');
string str1$ = fname.Mid(nn+1, 2)$;
type "1st number = %(str1$)";

string str2$ = fname.Between("-", "-")$;
type "2nd number = %(str2$)";

int nn = fname.ReverseFind('-');
int oo = fname.ReverseFind('.') ;
string str3$ = fname.Mid(nn + 1, oo - nn - 1)$;
type "3rd number = %(str3$)";

//Use %(string$) to convert string to number and do calculation 125-15*03
//Use $(number) to convert the calculation result to string and print
type $(%(str2$) - %(str1$) * %(str3$)); 

// Using string Registers, we can use substring notation
%M = "S15-125-03.dat";
%N = %[%M,2:3];  // Specify start and end
type "1st number = %N";
%N = %[%M,>'S']; // Find string after 'S'
%N = %[%N,'-'];  // Find remaining before '-'
type "1st number = %N";
%O = %[%M,#2,\x2D];  // Find second token delimited by '-' (hexadecimal 2D)
type "2nd number = %O";
%P = %[%M,'.'];  // trim extension
%P = %[%P,>'-']; // after first '-'
%P = %[%P,>'-']; // after second '-'
type "3rd number = %P";
type $(%O - %N * %P);

Getting a Substring from a Longer String Using String Variables

These examples show multiple ways to get a substring (in this case a file name) from a longer string (a full file path). For information on substring notation using string registers, see Substring Notation.

Find substring, using getFileName()

In this example, a string method designed for a very specific but commonly needed task is invoked.

// Use the built-in string method, GetFileName():
string fname$="C:\Program Files\Origin 8\Samples\Import\S15-125-03.dat";
string str1$ = fname.GetFileName()$;
str1$=;

Find substring, using reverseFind(), mid() methods

This time, a combination of string methods is used:

// Use the functions ReverseFind and Mid to extract the file name:
string fname$="C:\Program Files\Origin 8\Samples\Import\S15-125-03.dat";
// Find the position of the last '\' by searching from the right.
int nn=fname.ReverseFind('\');
// Get the substring starting after that position and going to the end.
string str2$=fname.Mid(nn+1)$;
// Type the file name to the Script Window.
str2$=;

Find substring, token-based

Here, another variation of generic finding methods is chosen to complete the task.

// Use a token-based method to extract the file name:

string fname$="C:\Program Files\Origin 8\Samples\Import\S15-125-03.dat";
// Get the number of tokens, demarcated by '\' characters.
int nn=fname.GetNumTokens('\');           
// Get the last token.
string str3$ = fname.GetToken(nn, '\')$;  
// Output the value of that token to the Script Window.
str3$=;


For more information, see String Methods.

Concatenate Strings

You can concatenate strings by using the '+' operator:

string aa$="reading";
string bb$="he likes " + aa$ + " books";
type "He said " + bb$;

You may also use the insert string method to concatenate two strings:

string aa$ = "Happy";
string bb$ = " Go Lucky";
// insert the string 'aa' into string 'bb' at position 1 
bb.insert(1,aa$);            
bb$=;

For a complete listing and description of supported string methods, please see String (Object).