robertjohns 0 Posted November 30, 2023 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
Marsil 4 Posted November 30, 2023 (edited) 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 December 1, 2023 by Marsil Changing code as question changed Share this post Link to post
DelphiUdIT 182 Posted November 30, 2023 There are others variant of that function, with options to replace all occurrences and case sensitive or not. See https://docwiki.embarcadero.com/Libraries/Athens/en/System.SysUtils.StringReplace for details. 1 Share this post Link to post
robertjohns 0 Posted December 1, 2023 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
David Heffernan 2352 Posted December 1, 2023 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. 3 Share this post Link to post
robertjohns 0 Posted December 1, 2023 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
Leif Uneus 43 Posted December 1, 2023 In short, you want to remove all strings in brackets, including the brackets. 2 Share this post Link to post
mvanrijnen 123 Posted December 1, 2023 Use this: System.SysUtils.TStringHelper.IndexOf - RAD Studio API Documentation (embarcadero.com) How to use the above to solve your problem is your job as a developer 😉 Share this post Link to post
robertjohns 0 Posted December 1, 2023 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
Marsil 4 Posted December 1, 2023 (edited) 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 December 1, 2023 by Marsil Share this post Link to post
mvanrijnen 123 Posted December 1, 2023 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
Marsil 4 Posted December 1, 2023 (edited) 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 December 1, 2023 by Marsil Share this post Link to post
Serge_G 87 Posted December 2, 2023 (edited) 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 December 2, 2023 by Serge_G added, code sample Share this post Link to post
David Heffernan 2352 Posted December 3, 2023 On 12/1/2023 at 5:01 AM, robertjohns said: This is (string which need to (remove) What should happen here Share this post Link to post
JohnLM 19 Posted December 3, 2023 Quote This is (string) which (string) need to (remove) ..or here. Share this post Link to post
Lars Fosdal 1792 Posted December 3, 2023 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
JohnLM 19 Posted December 3, 2023 (edited) 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 December 3, 2023 by JohnLM Share this post Link to post
Uwe Raabe 2061 Posted December 3, 2023 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
JohnLM 19 Posted December 3, 2023 (edited) 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 December 3, 2023 by JohnLM updated code snippet - removed Trim(), it does not work in this routine. Share this post Link to post
limelect 48 Posted December 6, 2023 (edited) Use RxStrUtils.pas large amount of functions that do exactly that Why invent the weel? Edited December 6, 2023 by limelect Share this post Link to post
Tom F 85 Posted December 7, 2023 @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! Share this post Link to post
pmcgee 21 Posted December 7, 2023 The regex solution above has an issue with nested parentheses ... but could be repeated. Share this post Link to post
pmcgee 21 Posted December 7, 2023 (edited) Well. I learnt about something today. Recursive regexes. Edited December 7, 2023 by pmcgee 2 Share this post Link to post
mvanrijnen 123 Posted December 7, 2023 49 minutes ago, pmcgee said: Well. I learnt about something today. Recursive regexes. double trouble 🙂 Share this post Link to post