Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing leading zeros from delphi string

Delphi 7

How do i remove leading zeros in a delphi string?

Example:

00000004357816

function removeLeadingZeros(ValueStr: String): String
begin
 result:= 
end;
like image 238
IElite Avatar asked May 02 '11 12:05

IElite


People also ask

How do you remove leading zeros from a string?

Use the inbuilt replaceAll() method of the String class which accepts two parameters, a Regular Expression, and a Replacement String. To remove the leading zeros, pass a Regex as the first parameter and empty string as the second parameter. This method replaces the matched value with the given string.

How do you remove leading zeros from an array?

Approach: Mark the first non-zero number's index in the given array. Store the numbers from that index to the end in a different array. Print the array once all numbers have been stored in a different container.


1 Answers

Searching for this in 2021 there is a much better solution that I found now and that is using the TStringHelper function TrimLeft as follows

myString := myString.TrimLeft(['0']);

see here for more on the documentation http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TStringHelper.TrimLeft

like image 123
StevenW Avatar answered Oct 04 '22 04:10

StevenW