Jump to content
Henry Olive

Delete substring from a string

Recommended Posts

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

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 by aehimself
  • Like 1

Share this post


Link to post

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!!! :classic_love:

 

LText will have always a valid value, same that empty!

 

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post

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;

image.png.f7a0ffeea2862b4ac39f42257db81767.png

Edited by programmerdelphi2k
  • Like 1

Share this post


Link to post

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.

  • Like 1

Share this post


Link to post
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

 

  • Like 1

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

×