Exportfile for AOT version 1.0 or later Formatversion: 1 ***Element: CLS ; Navision Axapta Class: String unloaded at Friday 1/12/2007 ; -------------------------------------------------------------------------------- CLSVERSION 1 CLASS #String PROPERTIES Name #String Extends # RunOn #Called from ENDPROPERTIES METHODS Version: 3 SOURCE #classDeclaration #/* 2001/06/18 by Greg Pierce # Simple string functions to fill in built-in function gaps # # Freely distributed and free to use as you please. # Tested in Axapta 3.0 and Dynamics AX 4.0 # # USAGE: static method have brief individual docs for massaging strings # */ # #class String #{ #} 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 #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::replaceAll( s, conpeek( forceLower, ix ), strlwr( conpeek( forceLower, ix ) ) ); # } # # // FIX UPPER CASE LIST # for( ix = 1; ix <= conlen( forceUpper ); ix++ ) # { # s = String::replaceAll( s, conpeek( forceUpper, ix ), strupr( conpeek( forceUpper, ix ) ) ); # } # # // DO SUBSTITUTIONS # for( ix = 1; ix <= conlen( subst ); ix++ ) # { # s = String::replaceAll( s, conpeek( conpeek( subst, ix ), 1 ), conpeek( conpeek( subst, ix ), 2 ) ); # } # # // FIX double spaces # if( flFixDoubleSpaces ) # { # while( String::contains( s, " " ) ) # { # s = String::replaceAll( s, " ", " " ); # } # } # # return s; # #} ENDSOURCE SOURCE #contains #/* return true if "s" contains the string "findStr" # */ # #static boolean contains( str s, str findStr ) #{ # if( strscan( s, findStr, 1, strlen( s ) ) > 0 ) # return true; # # return false; #} ENDSOURCE SOURCE #endsWith #/* returns true if "s" ends with "findStr" # */ # #static boolean endsWith( str s, str findStr ) #{ # str trim; # # trim = strdel( s, 1, strlen(s) - strlen(findStr) ); # # if( trim == findStr ) # return true; # # return false; #} 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 < ct; ix++ ) # { # s += char; # } # # return s; #} ENDSOURCE SOURCE #Main #public static void main( Args args ) #{ # str s = ""; # ; # # setPrefix( "String class examples" ); # # info( "String::beginsWith( \"abcdef\", \"abc\" ) -> " + ( String::beginsWith( "abcdef", "abc" ) ? "true" : "false" ) ); # # info( "String::endsWith( \"abcdef\", \"def\" ) -> " + ( String::endsWith( "abcdef", "def" ) ? "true" : "false" ) ); # # info( "String::fillString( 10, \"a\" ) -> " + String::fillString( 10, "a" ) ); # # info( "String::replace( \"abc123abc123\", \"abc\", \"def\" ) -> " + String::replace( "abc123abc123", "abc", "def" ) ); # # info( "String::replaceAll( \"abc123abc123\", \"abc\", \"def\" ) -> " + String::replaceAll( "abc123abc123", "abc", "def" ) ); # # info( "String::randomString( 8 ) -> " + String::randomString() ); # # info( "String::rSplit( \"abc|def\", \"|\" ) -> " + String::rSplit( "abc|def", "|" ) ); # #} 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 < _length; ix++ ) # { # nxt = int2str( random.nextInt() ); # random = new Random(); # s += strdel( nxt, 1, strlen( nxt ) - 1 ); # } # # 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 #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 #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 #split #/* return container with the value based on splitting the string at the delimiter # */ # #static container split( str _s, str _delimeter ) #{ # container c; # str s = _s, _value; # int pos = 1, loc; # # loc = strscan( s, _delimeter, 1, strlen( s ) ); # if( loc == 0 ) # { # c = conins( c, pos, s ); # return c; # } # # while( loc > 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 #splitName #/* split a first/last name # */ # #static container splitName( str name ) #{ # str fName, lastName; # int pos = strscan( name, " ", strlen( name ), 0 - strlen( name ) ); # # fName = strdel( name, pos, strlen( name ) - pos + 1 ); # lastName = strdel( name, 1, pos ); # # return [ fName, lastName ]; #} ENDSOURCE SOURCE #titleCase #/* 2002/06/07 by Greg Pierce # create a titlecased version of string # */ # #static str titleCase( str s, container exceptions = [" "] ) #{ # int ix; # str s1; # boolean fl = true; # # s = strltrim( strrtrim ( s ) ); # # for( ix = 1; ix <= strlen( s ); ix++ ) # { # s1 = strdel( s, ix + 1, strlen(s) - ix ); # s1 = strdel( s1, 1, ix - 1 ); # # if( confind( exceptions, s1 ) != 0 ) # { # fl = true; # continue; # } # # if( fl ) // beginning new word # { # if( strcmp( strupr( s1 ), s1 ) != 0 ) # { # s = strpoke( s, strupr( s1 ), ix ); # } # } # else // not a new word...just lower case it. # { # if( strcmp( strlwr( s1 ), s1 ) != 0 ) # { # s = strpoke( s, strlwr( s1 ), ix ); # } # } # fl = false; # } # # return s; # #} ENDSOURCE SOURCE #trimToLength #/* 2004/09/07 by Greg Pierce # 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 ) < _length ) # return _s; # # s = strdel( _s, _length + 1, strlen( _s ) - _length ); # # s += _trimStr; # # return s; #} ENDSOURCE ENDMETHODS ENDCLASS ***Element: END