Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-05

Trim repeated whitespace from a string

6 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

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
Cox, Stuart TRAN:EX

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
Felipe Monteiro de Carvalho
to Cox, Stuart TRAN:EX
The __CvtStr function used the _REM_LEAD_WHITE_STR + _REM_TRAIL_WHITE_STR

For this you can just use the Trim function.

+ _REDUCE_WHITE_STR

This looks easy to implement. You can just create your own function for this.

for i := 0 to Length(Str) do
begin
  If last character was a space and Str[i] is a space, take that character off.
end;
Felipe Monteiro de Carvalho
Jeff Pohlmeyer
Anybody know of a function that can remove (normalize) a string by remove all beginning and ending whitespace
uses SysUtils;
S:=Trim(S);
as well as replace all repeated spaces with a single space.
uses StrUtils;
S:=DelSpace1(S);
A bonus would be to remove tabs and newlines chars as well
I would convert them to spaces and let the functions above have them...
for i:=1 to length(S) do if ( S[i] in [#9..#13] ) then S[i]:=#32;

- Jeff

Michael Van Canneyt
to Graeme Geldenhuys: Anybody know of a function that can remove (normalize) a string by remove all beginning…
Graeme Geldenhuys wrote
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."

Use Trim from sysutils and then DelSpace1 from strutils.

http://www.freepascal.org/docs-html/rtl/strutils/delspace1.html

It might be a good idea to look at

http://www.freepascal.org/docs-html/rtl/strutils/index-5.html

Just so you know what is in store...

Michael.

Graeme Geldenhuys
to Michael Van Canneyt

Hi,

Michael Van Canneyt wrote 11 lines
Use Trim from sysutils and then DelSpace1 from strutils. http://www.freepascal.org/docs-html/rtl/strutils/delspace1.html It might be a good idea to look at http://www.freepascal.org/docs-html/rtl/strutils/index-5.html Just so you know what is in store... Michael.

Before all the replies, I managed to create my own custom function. I never knew about DelSpace1 or the StrUtils unit. Thanks everybody! I will go have a look at StrUtils unit and improve my own code if needed.

Regards,
Graeme.

There's no place like 127.0.0.1