Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Extracting strings of variable length? Post Reply Post New Topic
Author Message
Quirky
Newbie
Newbie
Avatar

Joined: 18 Jun 2012
Location: United Kingdom
Online Status: Offline
Posts: 8
Quote Quirky Replybullet Topic: Extracting strings of variable length?
     Posted: 18 Jun 2012 at 11:44pm
Hi all :)

I've got a string field (250chars long) which contains random notes written by multiple people. they all have their own shorthand, which doesn't help matters!

Anyways, it's possible that some records contain (1) "SS: 123456789" and/or "YN: ABCDEFGHIJKLMNOPQRSTTUVWXYZ SGYUB".

The SS details should be limited to nine numbers, but the YN section could take up the remaining 237chars. Having said that, it might only take up six; there's no way of knowing how much they'll type.

I want to be able to extract just the "123456789" and "ABCDEFGHIJKLMNOPQRSTTUVWXYZ SGYUB" parts of the field.

I tried using split({field}) and splitting by " ", but it insists the array is limited to [1].

If I split it using two spaces, it returns the entire note field.

Is there any way to find the start position of the YN field, figure out the length to the next space and then extract that section of the string?

Or does anyone know an easier way to do this?

Thanks (in advance :) )
-Q
IP IP Logged
rkrowland
Senior Member
Senior Member
Avatar

Joined: 20 Dec 2011
Location: England
Online Status: Offline
Posts: 259
Quote rkrowland Replybullet Posted: 19 Jun 2012 at 12:06am
Try this, it assumes there will always be 1 space contained inside the YN no ("YZ SGYUB").
 
stringvar str:="SS: 123456789 YN: ABCDEFGHIJKLMNOPQRSTTUVWXYZ SGYUB" & " ";
numbervar ss_s:= instr(str,"SS: ",1) + 4;
numbervar yn_s:= instr(str,"YN: ",1) + 4;
numbervar yn_m:= instr(yn_s,str," ")+1;
numbervar yn_e:= instr(yn_m,str," ");
stringvar SSno:= mid(str,ss_s,9);
Stringvar YNno:= mid(str,yn_s,yn_e-yn_s);
SSno //uncomment to show SS number
//YNno //uncomment to show YN number
 
edit the str variable to include your field, eg;
 
stringvar str:= {table.field} & " ";
 
Regards,
Ryan.


Edited by rkrowland - 19 Jun 2012 at 12:14am
IP IP Logged
Quirky
Newbie
Newbie
Avatar

Joined: 18 Jun 2012
Location: United Kingdom
Online Status: Offline
Posts: 8
Quote Quirky Replybullet Posted: 19 Jun 2012 at 12:46am
Oh, excellent :)

Thanks!
IP IP Logged
rkrowland
Senior Member
Senior Member
Avatar

Joined: 20 Dec 2011
Location: England
Online Status: Offline
Posts: 259
Quote rkrowland Replybullet Posted: 19 Jun 2012 at 1:07am
No probs, if you use this revised version it will identify fields where the numbers couldn't be found in the string rather than listing them as everything from character 4 to the next space.
 
stringvar err:= " yn: no match  ss: no match ";
stringvar str:="SS: 123456789 YN: ABCDEFGHIJKLMNOPQRSTTUVWXYZ SGYUB" & err;
numbervar ss_s:= instr(str,"SS: ",1) + 4;
numbervar yn_s:= instr(str,"YN: ",1) + 4;
numbervar yn_m:= instr(yn_s,str," ")+1;
numbervar yn_e:= instr(yn_m,str," ");
stringvar SSno:= mid(str,ss_s,9);
Stringvar YNno:= mid(str,yn_s,yn_e-yn_s);
SSno //uncomment to show SS number
//YNno //uncomment to show YN number
 
Regards,
Ryan.
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum