Henry Olive 5 Posted January 16, 2023 Good Day, var Str,SubStr : String; Str := could be '100' or Str := could be '100,101' or Str := could be '100,101,102' ..... I have another variable which indicates delete number SubStr :=100 ( this could be 101 or 102 ...) I want to delete SubStr from Str that is if Str = 100 then Result :='' if Str = 100,101 then Result :='101' if Str = 100,101,102 then Result :='101,102' If SubStr ='101' then then Result := '100,102' If SubStr ='102' then then Result := '100,101 Could someone please show me how to do ? Thank You Share this post Link to post
aehimself 396 Posted January 16, 2023 (edited) If you are sure they are always separated by commas, you can do Function GetRidOf(Const inString, inDeleteThis: String): String; Var mystr, s: String; Begin Result := ''; For s In inString.Split([',']) Do If s <> inDeleteThis Then Result := Result + s + ','; If Not Result.IsEmpty Then Result := Result.Substring(0, Result.Length - 1); End; This should properly keep '100' in '1100' for example. Edited January 16, 2023 by aehimself 1 Share this post Link to post
programmerdelphi2k 237 Posted January 17, 2023 (edited) you try this: '100,' or ',100' or ',100,' --> comma as determinant on expression Quote LText := LText.Replace('100,' , '', [rfReplaceAll]); //begin ... maybe not => 100, => 2100, or LText := LText.Replace(',100' , '', [rfReplaceAll]); //end ... maybe not => ,100 => ,1000 or LText := LText.Replace(',100,' , '', [rfReplaceAll]); //between if "comma" dont exists, dont worry! nothing will be changed! [rfReplaceAll] or [ ] for just 1 Quote if LText.Contains(',') then .... if LText.CountChar(',') > 1 then ... exists ",100," or anything similar ... you can use some like this: LText.Replace( LText.Replace(...), ... ) ... confused!!! LText will have always a valid value, same that empty! Edited January 17, 2023 by programmerdelphi2k 1 Share this post Link to post
programmerdelphi2k 237 Posted January 17, 2023 (edited) try this sample: function MyFindAndReplace2(const AStr: string; const ASubStr: string; const ADelimiter: Char): string; var LArr : TArray<string>; LNewArr: TArray<string>; begin result := AStr.Replace(' ', '', [rfReplaceAll]); // removing blank-spaces... // if (result.Trim = '') or (ASubStr.Trim = '') or (ADelimiter = #32) then exit(''); // LArr := result.Split([ADelimiter], TStringSplitOptions.ExcludeEmpty); // for var i: integer := 0 to high(LArr) do // only if not found! if (LArr[i] <> ASubStr) then LNewArr := LNewArr + [LArr[i]]; // result := ''.join(ADelimiter, LNewArr); end; procedure TForm1.Button3Click(Sender: TObject); var LText : string; LSubText: string; begin LText := ' 100,1000, 10, 1001, 101,102,100,,,,103,104, 100,100'; // ' '; // ' ,,'; // ' 1, ,'; LSubText := '100'; // Memo1.Text := LText; // Memo1.Lines.Add('Result=[' + MyFindAndReplace2(LText, LSubText, ',') + ']'); end; Edited January 17, 2023 by programmerdelphi2k 1 Share this post Link to post
David Schwartz 426 Posted January 17, 2023 You never stated whether there are always three digits in the list. What if you look to delete '100' from a list of (100, 110, 1001, 1009, 1100) ? Context can be important! You just gave a few samples without telling the whole story about everything that can possibly be in the list. 1 Share this post Link to post
Henry Olive 5 Posted January 17, 2023 Sorry, i should have informed that they are always separated by commas Thank you so much aehimself, programmer, David Share this post Link to post
David Schwartz 426 Posted January 19, 2023 On 1/17/2023 at 3:06 AM, Henry Olive said: Sorry, i should have informed that they are always separated by commas Thank you so much aehimself, programmer, David assuming you have a string that's just a comma-separated list of numbers like: var nums := '100, 101, 110, 1000, 1010, 1100, 200, 210'; just do something like this: var slist := TStringlist.Create; slist.CommaText := nums; if slist.IndexOf( '101' ) > 0 then // it's there else // it's not 1 Share this post Link to post