![Avatar of rincewind666](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
rincewind666
asked on
Remove word in string (500 points)
If a known word appears twice in a string or link, I want it removed:
For example, if a link is:
http://www.domain.com/docs/docs/example.htm (with the word "docs" mentioned twice)
I want it to become:
http://www.domain.com/docs/example.htm (with the word "docs" mentioned just once and removing the extra / symbol).
Note that I know the word so no need to discover if ANY word appears twice (easy). The word will always be "docs" or will have "docs" in it somewhere (docs, docs2, docs_one, etc).
I am using Delphi 6. This is URGENT so I am awarding 500 points and my grateful thanks.
For example, if a link is:
http://www.domain.com/docs/docs/example.htm (with the word "docs" mentioned twice)
I want it to become:
http://www.domain.com/docs/example.htm (with the word "docs" mentioned just once and removing the extra / symbol).
Note that I know the word so no need to discover if ANY word appears twice (easy). The word will always be "docs" or will have "docs" in it somewhere (docs, docs2, docs_one, etc).
I am using Delphi 6. This is URGENT so I am awarding 500 points and my grateful thanks.
![Avatar of bernani](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
Hi,
Sth like this:
Function ReplaceSubStringInStringNo
Var
CharPos : Integer;
LengthOldString : Integer;
UpperOldString : String;
Begin
Result := InputString;
LengthOldString := Length(OldSubString);
UpperOldString := UpperCase(OldSubString);
While True Do
Begin
CharPos := Pos(UpperOldString,UpperCa
If Not (CharPos = 0) Then
Begin
Delete(InputString,CharPos
Insert(NewSubString,InputS
End
Else
Begin
Break;
End;
End;
Result := InputString;
End;
Call like this
ReplaceSubStringInStringNo
wich returns:
http://www.domain.com/docs/example.htm
![Avatar of Mike Littlewood](https://filedb.experts-exchange.com/files/public/2013/11/18/b5a58f6e-4b00-402c-9ab6-9b797c9d27a9.png)
Need a few more examples really.
Could you have
http://www.domain.com/docs/docs_one/example.htm
http://www.domain.com/docs_one/docs/example.htm
http://www.domain.com/docs_one/docs_one/example.htm
http://www.domain.com/docs_next/docs_temp/example.htm
etc etc
ie. if they are not the same, which do you remove.
Could you have
http://www.domain.com/docs/docs_one/example.htm
http://www.domain.com/docs_one/docs/example.htm
http://www.domain.com/docs_one/docs_one/example.htm
http://www.domain.com/docs_next/docs_temp/example.htm
etc etc
ie. if they are not the same, which do you remove.
![Avatar of Mike Littlewood](https://filedb.experts-exchange.com/files/public/2013/11/18/b5a58f6e-4b00-402c-9ab6-9b797c9d27a9.png)
> The word will always be "docs" or will have "docs" in it somewhere
Does this also mean you could have
http://www.domain.com/mydocs/thisdocs/example.htm
Does this also mean you could have
http://www.domain.com/mydocs/thisdocs/example.htm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
![Avatar of bernani](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
Hi,
procedure TForm1.Button1Click(Sender
begin
Memo1.clear;
Memo1.lines.add(ReplaceSub
Memo1.lines.add(ReplaceSub
Memo1.lines.add(ReplaceSub
Memo1.lines.add(ReplaceSub
Memo1.lines.add(ReplaceSub
end;
gives always the same result whatever you use.
![Avatar of tammoz](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
hi
this function cuts S1 from S2 no matter how many times s1 is repeated in s2:
function StringCutter(S1, S2 :string) : string;
begin
while Ansipos(S1, s2) > 0 do
begin
ShowMessage(S2 + #13 + S1 + #13 + IntToStr(Ansipos(S1, s2)));
s2 := Copy(s2, 0,AnsiPos(s1, s2) - 1)
+ Copy(s2, AnsiPos(s1, s2) + Length(s1) + 1,Length(s2));
end;
Result := s2;
end;
this function cuts S1 from S2 no matter how many times s1 is repeated in s2:
function StringCutter(S1, S2 :string) : string;
begin
while Ansipos(S1, s2) > 0 do
begin
ShowMessage(S2 + #13 + S1 + #13 + IntToStr(Ansipos(S1, s2)));
s2 := Copy(s2, 0,AnsiPos(s1, s2) - 1)
+ Copy(s2, AnsiPos(s1, s2) + Length(s1) + 1,Length(s2));
end;
Result := s2;
end;
![Avatar of tammoz](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
if u want to keep one s1 just change this
function StringCutter(S1, S2 :string) : string;
var i : integer;
begin
i := Ansipos(S1, s2);
while Ansipos(S1, s2) > 0 do
begin
s2 := Copy(s2, 0,AnsiPos(s1, s2) - 1)
+ Copy(s2, AnsiPos(s1, s2) + Length(s1) + 1,Length(s2));
end;
S2 := Copy(s2, 0,i - 1) + S1 + Copy(s2, i -Length(s1) + 1,Length(s2));
Result := s2;
end;
function StringCutter(S1, S2 :string) : string;
var i : integer;
begin
i := Ansipos(S1, s2);
while Ansipos(S1, s2) > 0 do
begin
s2 := Copy(s2, 0,AnsiPos(s1, s2) - 1)
+ Copy(s2, AnsiPos(s1, s2) + Length(s1) + 1,Length(s2));
end;
S2 := Copy(s2, 0,i - 1) + S1 + Copy(s2, i -Length(s1) + 1,Length(s2));
Result := s2;
end;
![Avatar of rincewind666](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
ASKER
Many thanks for your help to all of you - greatly appreciated.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses StrUtils;
{$R *.dfm}
procedure TForm1.Button1Click(Sender
var
pos1,pos2: integer;
s,word1: string;
begin
word1 := 'docs/';
s := edit1.text;
pos1 := pos(word1,edit1.Text);
pos2 := posex(word1,edit1.text,pos
if pos2 > pos1 then
delete(s,pos1,length(word1
label1.Caption := s;
end;
end.