Jump to content

Elegant way to get leading text characters


This topic is 6992 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hi All,

I have a text field which contains basically random characters, alpha, numeric and symbol.

I need a calculation which returns characters only up to the first non-alpha character.

So:

"xyz2y" returns "xyz"

"y2r" returns "y"

"abc$tyu" returns "abc"

etc!

Another of those problems I expected to be easy, but have only come up with kludgy, long-winded solutions for.

Thanks...Sean!

Link to comment
Share on other sites

I won't be the judge of the elegancy in the here suggested method, but it gets the matter done:

I would start out with a repeating calc'field with as many repetitions as required - containing this:

Lets Call it WhereBreak[30]

Case (

1 xor Case ( Length ( Extend ( theText ) ) > Get ( CalculationRepetitionNumber );

Position ( "abcdefghijklmnopqrstuvwxyz" ; Middle ( Extend ( theText ) ; Get ( CalculationRepetitionNumber ) ; 1 ); 1; 1 ));Get ( CalculationRepetitionNumber ))

Then let us make a Result calc'field containing this:

Left ( theText; Min ( WhereBreak )-1)

Enjoy!!!

--sd

Link to comment
Share on other sites

Well it just occured to me that the break-char might be missing occationally .... hence:

Case (1 xor

Position ( "abcdefghijklmnopqrstuvwxyz" ; Middle ( Extend ( theText ); Get ( CalculationRepetitionNumber ) ; 1 ); 1; 1 );Get ( CalculationRepetitionNumber );1+Length(Extend ( theText )))

--sd

Link to comment
Share on other sites

If you are willing to write out all the potential non-alpha characters, you could use:

Let ( [

chars = "1234567890!@#$%^&*~" ;

first = Left ( Filter ( text ; chars ) ; 1 ) ;

pos = Position ( text ; first ; 1 ; 1 )

] ;

Left ( text ; pos - 1 )

)

If that's not an option:

Let ( [

dirty = Substitute ( text ;

[ "a" ; "" ] ;

[ "b" ; "" ] ;

[ "c" ; "" ] ;

// ... continue substitution for all alpha chars ...

[ "X" ; "" ] ;

[ "Y" ; "" ] ;

[ "Z" ; "" ] ) ;

dirty1 = Left ( dirty ; 1 ) ;

pos = Position ( text ; dirty1 ; 1 ; 1 )

] ;

Left ( text ; pos - 1 )

)

Link to comment
Share on other sites

Oops!

Should have specified this one is a FP6 question.

I'm happy to see it isn't that simple a trick!

Soren's option looks like it would work, but not as nice as I'd hoped smile.gif

It means I would have to make a guess at the maximum number of characters in a field, and there will be a lot of repeat fields empty!

I'll keep thinking about this one...

Link to comment
Share on other sites

In FM Developer 7, this would be a simple recursive calc. I can't actually write it at this moment, but here it is in pseudo-code:

LeadingText ( text ) =

If (the first character is a number ;

"" /* an empty string */ ;

Left ( text ; 1 ) & LeadingText ( all the characters in the original text except the first character )

Very simple, actually!! Give it a try. Recursion can be fun...

Link to comment
Share on other sites

Comment, you are a genious...

The following works a treat:

FirstAlphaCharacters=

Left( ALPHANUMERIC, Position( ALPHANUMERIC, Left( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Substitute( Lower(ALPHANUMERIC ), "z", ""), "y", ""), "x", ""), "w", ""), "v", ""), "u", ""), "t", ""), "s", ""), "r", ""), "q", ""), "p", ""), "o", ""), "n", ""), "m", ""), "l", ""), "k", ""), "j", ""), "i", ""), "h", ""), "g", ""), "f", ""), "e", ""), "d", ""), "c", ""), "b", ""), "a", ""), 1), 1, 1)-1)

I doubt there is any better solution.

Thanks!

Link to comment
Share on other sites

This topic is 6992 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.