Simple string functions to fill in built-in function gaps
Simple string functions to fill in built-in function gaps:
class INC_String
{
}
/* 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;
}
/* 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 = INC_String::replaceAll( s, conpeek( forceLower, ix ), strlwr( conpeek( forceLower, ix ) ) );
}
// FIX UPPER CASE LIST
for( ix = 1; ix <= conlen( forceUpper ); ix++ )
{
s = INC_String::replaceAll( s, conpeek( forceUpper, ix ), strupr( conpeek( forceUpper, ix ) ) );
}
// DO SUBSTITUTIONS
for( ix = 1; ix <= conlen( subst ); ix++ )
{
s = INC_String::replaceAll( s, conpeek( conpeek( subst, ix ), 1 ), conpeek( conpeek( subst, ix ), 2 ) );
}
// FIX double spaces
if( flFixDoubleSpaces )
{
while(INC_String::contains( s, " " ) )
{
s = INC_String::replaceAll( s, " ", " " );
}
}
return s;
}
/* 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;
}
/* 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;
}
/* 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;
}
public static void main( Args args )
{
str s = "";
;
setPrefix( "String class examples" );
info( "String::beginsWith( \"abcdef\", \"abc\" ) -> " + (INC_String::beginsWith( "abcdef", "abc" ) ? "true" : "false" ) );
info( "String::endsWith( \"abcdef\", \"def\" ) -> " + (INC_String::endsWith( "abcdef", "def" ) ? "true" : "false" ) );
info( "String::fillString( 10, \"a\" ) -> " + INC_String::fillString( 10, "a" ) );
info( "String::replace( \"abc123abc123\", \"abc\", \"def\" ) -> " + INC_String::replace( "abc123abc123", "abc", "def" ) );
info( "String::replaceAll( \"abc123abc123\", \"abc\", \"def\" ) -> " + INC_String::replaceAll( "abc123abc123", "abc", "def" ) );
info( "String::randomString( 8 ) -> " + INC_String::randomString() );
info( "String::rSplit( \"abc|def\", \"|\" ) -> " + INC_String::rSplit( "abc|def", "|" ) );
}
/* 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;
}
class INC_String
{
}
/* 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;
}
/* 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 = INC_String::replaceAll( s, conpeek( forceLower, ix ), strlwr( conpeek( forceLower, ix ) ) );
}
// FIX UPPER CASE LIST
for( ix = 1; ix <= conlen( forceUpper ); ix++ )
{
s = INC_String::replaceAll( s, conpeek( forceUpper, ix ), strupr( conpeek( forceUpper, ix ) ) );
}
// DO SUBSTITUTIONS
for( ix = 1; ix <= conlen( subst ); ix++ )
{
s = INC_String::replaceAll( s, conpeek( conpeek( subst, ix ), 1 ), conpeek( conpeek( subst, ix ), 2 ) );
}
// FIX double spaces
if( flFixDoubleSpaces )
{
while(INC_String::contains( s, " " ) )
{
s = INC_String::replaceAll( s, " ", " " );
}
}
return s;
}
/* 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;
}
/* 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;
}
/* 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;
}
public static void main( Args args )
{
str s = "";
;
setPrefix( "String class examples" );
info( "String::beginsWith( \"abcdef\", \"abc\" ) -> " + (INC_String::beginsWith( "abcdef", "abc" ) ? "true" : "false" ) );
info( "String::endsWith( \"abcdef\", \"def\" ) -> " + (INC_String::endsWith( "abcdef", "def" ) ? "true" : "false" ) );
info( "String::fillString( 10, \"a\" ) -> " + INC_String::fillString( 10, "a" ) );
info( "String::replace( \"abc123abc123\", \"abc\", \"def\" ) -> " + INC_String::replace( "abc123abc123", "abc", "def" ) );
info( "String::replaceAll( \"abc123abc123\", \"abc\", \"def\" ) -> " + INC_String::replaceAll( "abc123abc123", "abc", "def" ) );
info( "String::randomString( 8 ) -> " + INC_String::randomString() );
info( "String::rSplit( \"abc|def\", \"|\" ) -> " + INC_String::rSplit( "abc|def", "|" ) );
}
/* 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;
}
/* 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;
}
/* 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;
}
/* 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;
}
/* 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;
}
/* 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 ];
}
/* 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;
}
/* 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;
}
Comments
Post a Comment