Search code examples
syntaxcrystal-reports

Crystal Syntax - "Extract" Part of a String Within a String


I am doing this in i-net Designer (another Crystal Report type program).
My problem has two parts...

I am trying to "extract" part of a string from a string. The string that I need will always be in the middle of the main string. But will NEVER be in the same position. The Description and length will always change.

Below is an example.
Description (this is the main string I am "extracting" from):

    The Name of my Book Here CD CTN1234-5678-9 2-1 CD Carton

Part 1
The string I am trying to extract is the Part#, CTN1234-5678-9. This will always be 14 characters long(3 letters, 9 numbers and 2 dashes).

Part 2
I need the description portion only. This is the title of the book starting from the LEFT up to the Part#(CTN1234-5678-9). So I would need only The Name of my Book Here CD

Doing this in "Formula Editor" for my formula fields @PartNumber and @Description


Solution

  • I'm sure there is a simpler way- but this works:

    local stringVar myString   := 'The Name of my Book Here CD CTN1234-5678-9 2-1 CD Carton';
    local numberVar myPosition := len(myString);
    
    while not(mid(myString, myPosition - 14, 14) like '???????-????-?') and myPosition > 14
     do myPosition := myPosition - 1;
    
    mid(myString, myPosition - 14, 14);