Jump to content
robertjohns

Removing String

Recommended Posts

I have a string like

 

This is (string) which need to (remove)

 

How can I delete strings (string) and (remove) so that result should be

 

This is which need to

 

 

 

 

 

Share this post


Link to post
  var s:= 'This is (string) which need to (remove)';
  var LeftIndex, RightIndex : Integer;

  LeftIndex := S.IndexOf (' (');
  while LeftIndex > -1 do
    begin
      RightIndex := S.IndexOf (')');
      if RightIndex = -1 then
        Break;
      S := S.Remove (LeftIndex, RightIndex - LeftIndex + 1);
      LeftIndex := S.IndexOf (' (');
    end;
    

 

Edited by Marsil
Changing code as question changed

Share this post


Link to post
7 hours ago, Marsil said:

  var s := 'This is (string) which need to (remove)';
  S := S.Replace (' (string)', '').Replace (' (remove)', '');
  Log ('S=' + s); // S=This is which need to

 

This is basic and you have tried to replace the string you already know.

 

String can be anything in brackets and need to delete the string including brackets .. all the string occurrences in line which is with bracket need to delet

Share this post


Link to post

Your specification in the original post is incomplete so it's not surprising that people don't know what you want. Your clarification is still unclear. 

 

Until you can define precisely what you want the code to do how could you expect anyone, even yourself, to be able to write it. 

  • Like 3

Share this post


Link to post
34 minutes ago, David Heffernan said:

Your specification in the original post is incomplete so it's not surprising that people don't know what you want. Your clarification is still unclear. 

 

Until you can define precisely what you want the code to do how could you expect anyone, even yourself, to be able to write it. 

Thanks a lot for reply

 

I have Memo line in which there are such strings which are not specific otherwise I can use StringReplace method ,strings are in brackets () and I want to delete those strings which are with brackets

 

suppose

 

This is (string) which need to (remove)

 

Need to delete (string and (remove) from the above

 

Share this post


Link to post
2 hours ago, Leif Uneus said:

In short, you want to remove all strings in brackets, including the brackets.

Yes right need to remove strings in brackets including brackets

Share this post


Link to post
25 minutes ago, robertjohns said:

Yes right need to remove strings in brackets including brackets

As you changed your question I changed my answer above, I Hope it's what you are looking for.

BTW,  Your question has nothing to do with VCL, You should have posted it in General Help forum.

Edited by Marsil

Share this post


Link to post
17 minutes ago, Marsil said:

As you changed your question I changed my answer above, I Hope it's what you are looking for.

BTW,  Your question has nothing to do with VCL, You should have posted it in General Help forum.

and now try this ) with this ( text as it )() fill fail (sometimes) 😉

 

Share this post


Link to post
11 minutes ago, mvanrijnen said:

and now try this ) with this ( text as it )() fill fail (sometimes) 😉

 

I don't understand what you just said!.

 

As the OP did not completely describe what the input data will be, The answer is not meant to be valid for any data input.

It's just a sample describing how to remove occurrences from a string. Can be adapter by OP to his own needs as he wish.

If you have not noticed I didn't write this as ready to use function , Just a sample code snippet.

Edited by Marsil

Share this post


Link to post

Did you consider using regular expressions ?

uses system.regularexpressions;

procedure TForm1.Button1Click(Sender: TObject);
var reg : TRegEx;
    r : String;
const s = 'This is (string) which need to (remove)';
begin
reg:=TRegEx.Create('\([^()]*\)');
r:=reg.Replace(s,'');
showmessage(r);
end;

 

Edited by Serge_G
added, code sample

Share this post


Link to post
Quote

This is (string) which (string) need to (remove)

..or here.

Share this post


Link to post

My interpretation:
This is (string which need to (remove) -> This is

This is (string) which (string) need to (remove) -> This is which need to

Share this post


Link to post

Serge_G's routine works, including when multiples of, i.e., (string) .. (string) 

 

But it does not remove the extra spaces. And Trim() does not remove the spaces in-between char/strings unless the original string includes spaces at the first and last position, i.e., 

 

Trim('  This is (string) which (string) need to (remove)  ')  ---  works

Trim('This is (string)   which (string) need   to (remove)')  ---  fails 

 

So, the routine would require more work/additional code.

 

Edited by JohnLM

Share this post


Link to post

I would split removing the bracketed strings and removing double blanks in separate methods.

 

I am assuming the strings are well-formed here, which means that each opening bracket matches a closing bracket, no orphaned brackets and no nesting. Otherwise use one of the other algorithms shown above.

 

function RemoveInBrackets(const AString: string): string;
begin
  var arr := AString.Split(['(', ')']);
  for var I := High(arr) downto 0 do
  begin
    if Odd(I) then
      Delete(arr, I, 1);
  end;
  Result := string.Join('', arr);
end;

function RemoveDoubleBlanks(const AString: string): string;
begin
  Result := string.Join(' ', AString.Split([' '], TStringSplitOptions.ExcludeEmpty));
end;

 

 

Share this post


Link to post

Here is my version of a (string) removal for this practice: 

 

function StrRemove(s: string; StrToRemove:string): string;
begin
  while pos(strToRemove,s,1) <> 0 do begin // recursive
    if pos(strToRemove, s,1)  > 0 then delete(s,pos(strToRemove,s,1),length(strToRemove));
  end;
  result := s;
end;

// call with 

var s: string; 
begin
  s := 'This is (string) which (string) need to (remove).'; 
  s := StrRemove(s,'(string)');
  s := StrRemove(s,'(remove)');
end; 

 

Edited by JohnLM
updated code snippet - removed Trim(), it does not work in this routine.

Share this post


Link to post

Use RxStrUtils.pas large amount of functions that do exactly that

Why invent the weel?

Edited by limelect

Share this post


Link to post

@Lars Fosdal  Ha!  I actually literally LOL-ed on that.  

 

ChatGPT has changed my relationship to RegEx. As we know, it's not always correct, but, hey, what a great tool!


image.thumb.png.3498940bc085cf67c7f106b2c252eeff.png

 

 

 

Share this post


Link to post

The regex solution above has an issue with nested parentheses ... but could be repeated.
image.thumb.png.289431d940156f323f7f9cf16d722172.png

Share this post


Link to post

Well. I learnt about something today. Recursive regexes.

image.thumb.png.517cfa8968df96f33e0511fd7cc79d6d.png

Edited by pmcgee
  • Like 2

Share this post


Link to post
49 minutes ago, pmcgee said:

Well. I learnt about something today. Recursive regexes.

double trouble 🙂

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×