Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-09-17
12
Medium Priority
?
649 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:hidrau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 33701116
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;
0
 
LVL 19

Expert Comment

by:Thommy
ID: 33701296
You can check functionality with the attached program...
RemoveMoreThanOneSpacesAroundWor.exe
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 33701315
just use stringreplace

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

Only double spaces will be changed into single spaces
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 33701324
you could run it multiple times if you have more than 2 spaces
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 33701343
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
0
 
LVL 19

Accepted Solution

by:
Thommy earned 2000 total points
ID: 33701466
Ok, I will not post .exe in future!!!

Here's another version of my function using "stringreplace":

function RemoveMoreThanOneSpacesBetweenWords02(s:string):string;
var
  p:integer;
begin
  s:=trim(s);
  Repeat
    p:=pos('  ',s);
    if p>0 then s := StringReplace(s, #32#32, #32, [rfReplaceAll]);
  Until p=0;

  result:=s;
end;
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 33701532
thanks very much
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 33701559
hidrau ...
you normally share poinx when 1 person gives the idea
and the other one steals the idea
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 33701902
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.  
0
 
LVL 19

Expert Comment

by:Thommy
ID: 33702303
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...

0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 33702718
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.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 33704759
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question