Recent

Author Topic: How to properly trim a string in FPC  (Read 4731 times)

kveroneau

  • Full Member
  • ***
  • Posts: 119
How to properly trim a string in FPC
« on: February 09, 2016, 07:55:59 pm »
Hello, firstly, sorry for the somewhat stupid question, as I believe there is a rather simple answer for this, and I'm just thinking about it too much.  I was programming in Python for many years, and string manipulation is rather trivial in dynamic languages.  So, here's a snippet of code I am having trouble with:

RunCommand('/usr/bin/which', [cmd], output);

So, if anybody has used the which command, it provides the exact location of a binary within the path.  I am actually trying to combine this with other Process commands, anyways.  My issue here is that the which command appends a "\n" at the end, a new line, if you will.  So, in order to pass this string to say RunCommand, this newline needs to be removed.  Normally in Python, I would just use string slices to take the last character off, but in FPC, I am unsure of the exact method to get this done.

output is an ansistring, so from my understanding, it's a pointer to a string on the heap.  At first I was thinking of converting it over to a PChar, and working with the functions in the strings unit, but I am thinking there has to be a better way of doing this.  Can anybody shed some light on the best practices of string manipulation in Pascal/Delphi/Lazarus?

Many thanks.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11446
  • FPC developer.
Re: How to properly trim a string in FPC
« Reply #1 on: February 09, 2016, 08:05:03 pm »

From unit strutils use

Code: [Select]
  removetrailingchars(s,[#13,#10]);

kveroneau

  • Full Member
  • ***
  • Posts: 119
Re: How to properly trim a string in FPC
« Reply #2 on: February 09, 2016, 08:50:09 pm »
Thanks!  I knew I was over thinking it and there was a helper function somewhere.  Something like string manipulation shouldn't be complex anymore in modern static languages.  ;)

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: How to properly trim a string in FPC
« Reply #3 on: February 10, 2016, 05:12:35 pm »
Also take a look at Trim(), TrimLeft() and TrimRight().

Bart

 

TinyPortal © 2005-2018