Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with InStr to Print Only First Portion of a String

Status
Not open for further replies.

JonAtHQ

Technical User
Jun 16, 2005
45
US
I have strings such as:

The sky, is blue, ID_123
The grass is green, ID_345
All trees have leaves, PW_456
Tek-Tips is Great, YY_331

I would like to eliminate the identification code and print out only:

The sky, is blue
The grass is green
All trees have leaves
Tek-Tips is Great

Considerations:

1) There might be commas in the string I want to print.
2) I will know all the possibilities following the commas. So, I know that the strings will be limited to having "ID", "PW", or "YY".
3) There will never be an identification code such as "ID_123" embedded in the string I want to keep".

I've tried LEFT, InStr and a combination of the two. Your help will be greatly appreciated.

btw... Crystal XI
 
stringvar x := {table.field};
stringvar array y := split(x,",");
stringvar z := "";
numbervar i;
numbervar j := ubound(y);
for i := 1 to j-1 do(
z := z + y + ", "
);
z

-LB
 
LB,

Thanks for your reply.

Will your suggestion work for strings that have commas that I want to keep? For example,

"The sky, is blue, ID_123" should print out as "The sky, is blue"

-JB
 
Just change the last line of the formula to the following to remove the final comma (Other commas will be there):

if len(x)>1 then
left(z,len(z)-1)

-LB
 
Hi again. Which is "the last line" that should be replaced? Am I replacing one line of formula with two lines? Is it "z" that I replace?
 
Follow-up to my last question: The initial formula works (but keeps the final comma). When I replace the last line "z" with the two lines above, Crystal Reports displays the following error, "String length is less than 0 or not an integer.
 
z.

stringvar x := {table.field};
stringvar array y := split(x,",");
stringvar z := "";
numbervar i;
numbervar j := ubound(y);
for i := 1 to j-1 do(
z := z + y + ", "
);
if len(x)>1 then
left(z,len(z)-1)

-LB
 
Sorry that should have been:

stringvar x := {table.field};
stringvar array y := split(x,",");
stringvar z := "";
numbervar i;
numbervar j := ubound(y);
for i := 1 to j-1 do(
z := z + y + ", "
);
if len([red]z[/red])>1 then
left(z,len(z)-1)

-LB
 
I solved the problem by doing z-2 (instead of 1):

left(z,len(z)-2)

Thanks!
 
Yes, it should have been 2--forgot I added a space after the comma.

-LB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top