This topic is 6601 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!

##### 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

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

##### 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

##### Share on other sites

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

Let ( [

chars = "[email protected]#\$%^&*~" ;

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 )

)

##### 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 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!

##### 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:

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

##### Share on other sites

Oops indeed!

My second suggestion will work with prior versions - after you rewrite it to eliminate the Let() and nest the Substitutes.

##### 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!

##### Share on other sites

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

## Create an account

Register a new account