Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

exist a delphi function that could remove more than one space from words?

Hello guys,

I have a table with 10.000 lines with sentences, just like this:

"My father is working at home"

Sometimes, there are sentences with double or more space among words, exemplo:

"She  is here  with Vanessa."

I need a function that could fix it, removing more than one space among words.

thanks
alexandre
Avatar of Thommy
Thommy
Flag of Germany image

There is no Delpi standard function, which will do that.

But I have written a function on my own. Try, if it works for your matters:

function RemoveMoreThanOneSpacesBetweenWords(s:string):string;
var
  p:integer;
begin
  s:=trim(s);
  Repeat
    p:=pos('  ',s);
    if p>0 then delete(s,p+1,1);
  Until p=0;

  result:=s;
end;
You can check functionality with the attached program...
RemoveMoreThanOneSpacesAroundWor.exe
Avatar of Geert G
just use stringreplace

Text := StringReplace(Text, #32#32, #32, [rfReplaceAll]);

Only double spaces will be changed into single spaces
you could run it multiple times if you have more than 2 spaces
thommy, don't post .exe to see a sample working
it'll just fill up the ee servers

post a code sample instead showing the usage
ASKER CERTIFIED SOLUTION
Avatar of Thommy
Thommy
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

thanks very much
hidrau ...
you normally share poinx when 1 person gives the idea
and the other one steals the idea
Just out of curiosity, you reference having the data in a TABLE . . . what is the database?
If this is a SQL SERVER table, then you can execute a query to accomplish the substitution of one space for multiple spaces and it would execute a lot faster than doing it row by row.  I believe there is also a corresponding SQL function in most other databases.  
8080_Diver is right.

For example in ORACLE and SQL Server you can use the REPLACE()-function in an Update-Command to do string replacement for fixed character strings.

But if you have different strings to replace like double or triple spaces, then you either have to write a SQL function or start your update command several times to get all multi-spaces replaced by one space...

Thommy,
If you put the process in a stored proc and have it in a WHILE loop, you can execute it until you get 0 rows affected.  It is still a lot faster than doing it on a row by row basis. ;-)
Remember, SQL Is Your Friend.
you actually run it until the length of the text is the same before and after the function
the number of rows doesn't need to change