Blaise Computing's "Power Tools Plus" offered just such a function in their Unit_Str unit.
The __CvtStr function used the _REM_LEAD_WHITE_STR + _REM_TRAIL_WHITE_STR + _REDUCE_WHITE_STR combination of constants to achieve what you'd like to do.
I have the full source to all their units, but, as the copywrite says, it's theirs. What are the legalities when a company goes kaput like this? Is the source still only usable by those that bought a licence, like me?
Header follows:
{*********************************************************************** }
{
}
{ UNIT_STR String Functions
}
{
}
{ This unit implements the POWER TOOLS PLUS String manipulation }
{ routines. These routines enhance the string functions provided by }
{ Turbo Pascal. All routines in this unit are functions which return }
{ the generic string type.
}
{
}
{ Justification constants for __JustStr.
}
{
}
{ _LEFT_JUST_STR Left justify (constant) }
{ _RIGHT_JUST_STR Right justify (constant) }
{ _CENTER_STR Center within the field (constant) }
{
}
{ String converstion constants for __CvtStr.
}
{
}
{ _REM_WHITE_STR Remove all white space (constant) }
{ _REM_LEAD_WHITE_STR Remove leading white space (constant) }
{ _REM_TRAIL_WHITE_STR Remove trailing white space (constant) }
{ _REDUCE_WHITE_STR Reduce white space to one blank (constant) }
{ _SAVE_QUOTED_STR Preserve quoted string (constant) }
{ _TO_UPCASE_STR Convert to upper case (constant) }
{ _TO_LOWCASE_STR Convert to lower case (constant) }
{ _DISCARD_STR Discard excess characters (constant) }
{
}
{ Date formatting constants
}
{
}
{ _USA_DT_STR USA format mm-dd-yy (constant) }
{ _EURO_DT_STR European format dd-mm-yy (constant) }
{ _YEAR_DT_STR Full year format mm-dd-yyyy (constant) }
{ _MONT_DT_STR Month abbrv. format dd-mmm-yy (constant) }
{ _FORM_DT_STR Formal date, e.g., June 1, 2988 (constant) }
{
}
{ Time formatting constants
}
{
}
{ _12HOUR_STR Twelve hour time string, "1:25" (constant) }
{ _INC_SEC_STR Include seconds, "13:25:30" (constant) }
{ _INC_TIC_STR Include hundredths, "13:25.55" (constant) }
{ _INC_AMPM_STR Append AM or PM, "13:25 PM" (constant) }
{ _STANDARD_STR Standard time string, "1:25 PM" (constant) }
{ _COMPLETE_STR Complete time string, "13:25:30.55" (constant) }
{ _DOS_DIR_STR Used by __DirStr "1:25p" (constant) }
{
}
{ _AMPM_STR The strings ' AM' and ' PM' (static) }
{ _ap_STR The string 'ap' for DOS time (static) }
{
}
{ _FMT_BUFLEN_STR Format text file buffer length (constant) }
{
}
{ _StrMonths Array of month names (static) }
{ _StrDays Array of day names (static)
}
{
}
{ _StrUSACh Date delimiter for USA (static) }
{ _StrEuroCh Date delimiter for Europe (static) }
{
}
{ _StrMoneyCh Money character (static)
}
{
}
{ __LeftStr Extract the left most characters of a string }
{ __RightStr Extracts characters to the right end of a string }
{ __RightMost Extract the right most characters of a string }
{ __SubStr Extract substring from a string
}
{ __MidStr Place characters within a string
}
{ __FillStr Fill a string with copies of a character. }
{ __XlateStr String translation
}
{ __JustStr Justify a string within a field.
}
{ __CvtStr Conversion of a string
}
{ __EnTabStr Return a string with spaces converted to tab characters }
{ __DeTabStr Return a string with tabs expanded to space characters }
{ __ToRadStr Return the radix representation of an integer type }
{ __ToDecStr Return the unsigned decimal representation of an integer }
{ __ToHexStr Return the hexadecimal representation of an integer }
{ __Ptr2Str Return a string representing a pointer value }
{ __DateStr Return the system date
}
{ __TimeStr Return the system time
}
{ __FormStr Format a string according to a mask
}
{ __Dt2StStr Convert month, day and year into a date string }
{ __St2DtStr Extract the month, day, and year from a date string }
{ __InitFStr Initialize a string format text file device. }
{ __RetBfStr Return the formatted text buffer.
}
{ __DirStr Convert DOS directory information to a string }
{ __Time2Str Make a string representing the time of day }
{ __TFromStr Return the time from a string
}
{
}
{ Version 5.10 (C)Copyright Blaise Computing Inc. 1990 }
{_______________________________________________________________________ }
Stu
Earlier message 24 lines
-----Original Message-----
From:
On Behalf Of Graeme
Geldenhuys
Sent: Fri, May 26, 2006 7:08 AM
To: FPC-Pascal users discussions
Subject: [fpc-pascal] Trim repeated whitespace from a string
Hi,
Anybody know of a function that can remove (normalize) a string by
remove all beginning and ending whitespace as well as replace all
repeated spaces with a single space. A bonus would be to remove tabs
and newlines chars as well, but that is not so important.
Example:
Before: " This is what it should look like. "
After: "This is what it should look like."
Any ideas? I see JavaScript has such a build-in function called
--
There's no place like 127.0.0.1