# 2.6.2 String Processing

### Using String Methods

These examples show multiple ways to get a substring (in this case a file name) from a longer string (a full file path). In the last of these, we demonstrate how to concatenate two strings.

#### 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$=;

### String Concatenation

You can concatenate strings by using the '+' operator. As shown below:

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).

### Using String Registers

String Registers are simpler to use and quite powerful, but more difficult to read when compared with string variables and their methods. Also, they are global (session scope) and you will have less control on their contents being modified by another program.

// 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=;

### Extracting Numbers from a String

This 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$)";

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);