Jump to content
Henry Olive

Delete First & Last Character

Recommended Posts

Good Day

 

Str := 'Abc' or could be Abc

First,  I want check if first char = ' then i want to delete it

Second  I want check if Last char = ' then i also want to delete it

that is,  if Str='Abc' then the result should be Abc 

if Str = Abc then i dont need to do anything

 

Thank You

 

Share this post


Link to post

There is a lot of information and documentation available about string handling in Delphi. Here are two quick references:

 

http://www.delphibasics.co.uk/ByFunction.php?Main=Strings

https://www.thoughtco.com/string-handling-routines-delphi-programming-4092534

 

And here is a very basic solution to your request. Your actual purpose may be different from your description, but this code does what you asked:

 

  var str := '''abc''';
  WriteLn(str);
  if str[1] = '''' then
    Delete(str, 1, 1);
  var last := Length(str);
  if str[last] = '''' then
    Delete(str, last, 1);
  WriteLn(str);
  ReadLn;

 

Share this post


Link to post
42 minutes ago, Henry Olive said:

First,  I want check if first char = ' then i want to delete it

Second  I want check if Last char = ' then i also want to delete it

The SysUtils unit in Delphi's RTL has AnsiDequotedStr() and AnsiExtractQuotedStr() functions for that exact purpose.

  • Like 2

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

×