Monday, April 26, 2010

String function class

While working on string function i have found an nice code on Greg Pierce's blog extending existing AX string function functionality to construct newer method above post is in AX 3.0 and 4.0 below code is in AX 2009

COPY PASTE THE CODE IN NOTE PAD AND SAVE AS XPO.

Exportfile for AOT version 1.0 or later
Formatversion: 1

***Element: CLS

; Microsoft Dynamics AX Class: String_Func unloaded
; --------------------------------------------------------------------------------
CLSVERSION 1

CLASS #String_Func
Id 30211
PROPERTIES
Name #String_Func
Extends #
RunOn #Called from
ENDPROPERTIES

METHODS
Version: 3
SOURCE #trimToLength
#/* trim a string to a length, optionally add ellipsis if string is truncated.*/
#public static str trimToLength( str _s, int _length, str _trimStr = "" )
#{
# str s;
#
# if( strlen( _s ) < s =" strdel(" exceptions =" [" fl =" true;" s =" strltrim(" ix =" 1;" s1 =" strdel(" s1 =" strdel(" fl =" true;" s =" strpoke(" s =" strpoke(" fl =" false;" pos =" strscan(" fname =" strdel(" lastname =" strdel(" s =" _s," pos =" 1," loc =" strscan(" loc ="=" c =" conins("> 0 )
# {
# _value = strdel( s, loc, strlen(s) - loc + 1 );
#
# if( _value && _value != _delimeter )
# {
# c = conins( c, pos, _value );
# pos++;
# }
# s = strdel( s, 1, loc );
#
# loc = strscan( s, _delimeter, 1, strlen( s ) );
# }
# if( s && s != _delimeter )
# c = conins( c, pos, s );
#
# return c;
#
#}
ENDSOURCE
SOURCE #rSplit
#/* split item of right of string, trimmed at delimiter*/
#static str rSplit( str _s, str _delimeter )
#
#{
# str s = "";
# int pos;
# int len = strlen( _s );
#
# pos = strfind( _s, _delimeter, len, -len );
# s = strdel( _s, 0, pos );
#
#
# return s;
#}
ENDSOURCE
SOURCE #replaceAll
#/* replace the all occurances of "findStr" with "replStr" in "s"*/
#
# static str replaceAll( str s, str findStr, str replStr )
# {
# int pos = strscan( s, findStr, 1, strlen( s ) );
#
# while( pos > 0 )
#
# {
# s = strdel( s, pos, strlen( findStr ) );
# s = strins( s, replStr, pos );
#
# pos = strscan( s, findStr, pos + strlen( replStr ), strlen( s ) );
#
# }
#
# return s;
# }
ENDSOURCE
SOURCE #replace
#/* replace the first occurance of "findStr" with "replStr" in "s"*/
#
#static str replace( str s, str findStr, str replStr )
#
#{
# int pos = strscan( s, findStr, 1, strlen( s ) );
#
# if( pos > 0 )
# {
# s = strdel( s, pos, strlen( findStr ) );
# s = strins( s, replStr, pos );
#
# }
#
# return s;
#}
ENDSOURCE
SOURCE #randomString
#/* build a random string of length passed*/
#public static str randomString( int _length = 6 )
#{
# str s;
# int ix = 0;
# str nxt;
# RandomGenerate random = new RandomGenerate();
# ;
#
# for( ix = 0; ix < nxt =" int2str(" random =" new" s = ""> " + ( String_Func::beginsWith( "abcdef", "abc" ) ? "true" : "false" ) );
#
#
# info( "String_Func::endsWith( \"abcdef\", \"def\" ) -> " + ( String_Func::endsWith( "abcdef", "def" ) ? "true" : "false" ) );
#
#
# info( "String_Func::fillString( 10, \"a\" ) -> " + String_Func::fillString( 10, "a" ) );
#
# info( "String_Func::replace( \"abc123abc123\", \"abc\", \"def\" ) -> " + String_Func::replace( "abc123abc123", "abc", "def" ) );
#
#
# info( "String_Func::replaceAll( \"abc123abc123\", \"abc\", \"def\" ) -> " + String_Func::replaceAll( "abc123abc123", "abc", "def" ) );
#
#
# info( "String_Func::randomString( 8 ) -> " + String_Func::randomString() );
#
# info( "String_Func::rSplit( \"abc|def\", \"|\" ) -> " + String_Func::rSplit( "abc|def", "|" ) );
#
#
#}
ENDSOURCE
SOURCE #fillString
#/* build a repeating string of length ct
# */
#
#public static str fillString( int ct, str char = " " )
#
#{
# int ix;
# str s = "";
# ;
#
# for( ix = 0; ix < trim =" strdel(" trim ="="> 0 )
# return true;
#
# return false;
#
# }
ENDSOURCE
SOURCE #cleanString
#/* use to do multiple string manipulations on the string passed, including case forcing and substitutions */
#
# static str cleanString( str s, container forceLower, container forceUpper, container subst, boolean flFixDoubleSpaces = true )
# {
# int ix;
#
#
# // FIX LOWER CASE LIST
# for( ix = 1; ix <= conlen( forceLower ); ix++ )
# {
# s = String_Func::replaceAll( s, conpeek( forceLower, ix ), strlwr( conpeek( forceLower, ix ) ) );
#
# }
#
# // FIX UPPER CASE LIST
# for( ix = 1; ix <= conlen( forceUpper ); ix++ )
# {
# s = String_Func::replaceAll( s, conpeek( forceUpper, ix ), strupr( conpeek( forceUpper, ix ) ) );
#
# }
#
# // DO SUBSTITUTIONS
# for( ix = 1; ix <= conlen( subst ); ix++ )
# {
# s = String_Func::replaceAll( s, conpeek( conpeek( subst, ix ), 1 ), conpeek( conpeek( subst, ix ), 2 ) );
#
# }
#
# // FIX double spaces
# if( flFixDoubleSpaces )
# {
# while( String_Func::contains( s, " " ) )
# {
# s = String_Func::replaceAll( s, " ", " " );
#
# }
# }
#
# return s;
#
# }
ENDSOURCE
SOURCE #beginsWith
#/*return true if "findStr" is the inital character match for "s" */
#
# static boolean beginsWith( str s, str findStr )
#
# {
# if( strscan( s, findStr, 1, strlen( s ) ) == 1 )
# return true;
#
# return false;
# }
ENDSOURCE
SOURCE #classDeclaration
#public class String_Func
#{
#}
ENDSOURCE
ENDMETHODS
ENDCLASS

***Element: END

No comments: