You are here

Different String type verification functions in Delphi

Submitted by Asif Nowaj, Last Modified on 2019-11-22

Different String type verification functions in Delphi

If you want to check whether a string is an empty string, use below function. If a string is composed by multiple spaces or nil then also this function will return as true to tell that string is empty. Please use this function accordingly. If you want to use this function for WideString then you need to create an overloaded function for WideString.
Here Delphi in-built Trim function is used. To know more on Trim, see Different Types of Trim Functions in Delphi.

If you are searching for a better way to check for an empty string in Delphi, this is the function that you are looking for. This is similar to IsEmpty function. It checks whether the string is empty or not.
String Version:


function IsStringEmpty(s: string): Boolean;
begin
  Result := Trim(s) = '';
end;

WideString version:


function IsStringEmpty(s: Widestring): Boolean;
begin
  Result := Trim(s) = '';
end;

To know whether a string is an integer use the below function. In Delphi check if string is integer, you can use below function. It is using Delphi TryStrToInt function.


function IsStringInt(s: string): Boolean;
var
  v: integer;
begin
  Result := TryStrToInt(s, v);
end;

To know whether a string is a hexadecimal or not, use below function. Here it is assuming that a legal hex number should start with either $ or 0x. In Delphi, check if string is Hex or not through this function. . If you want to know how to convert string to Tcolor in Delphi or TColor to string you can follow this link different string conversion function in Delphi like Bool to String, String to Bool, Color to string, string to Color, FontStyles to String and String to FontStyles.


function IsStringHex(s: string): Boolean;
var
  i, l: integer;
begin
  l := Length(s);
  if l < 2 then
  begin
    Result := false;
    exit;
  end;

  Result := s[1] = '$';
  for i := 2 to l do
    if (s[i] in ['A'..'F', 'a'..'f', '0'..'9']) = false then
    begin
      Result := false;
      exit;
    end;
end;

If you want to know or search whether a string is present within an array of strings then use the below function. This function is written using wide string so that this can used for string too. So if your question is How do I check whether a string exists in an string array? use this below function. I think this is the best way in delphi - Best way to find if a string is in a string array (without generics).


function StringInStringArray(s: WideString; StringArray: array of WideString): Boolean;
var
  i: integer;
begin
  for i := Low(StringArray) to High(StringArray) do
    if s = StringArray[i] then
    begin
      Result := true;
      exit;
    end;

  Result := false;
end;

To know whether a string is in the list of colors or a representation of color code, use below function. Using this function you will be able to find or search whether a string is within a list of color value or the color is represented in Hex form. If you want to know how to convert string to Tcolor in Delphi or TColor to string you can follow this link different string conversion function in Delphi like Bool to String, String to Bool, Color to string, string to Color, FontStyles to String and String to FontStyles.


function StringIsColor(s: string): Boolean;
begin
  Result := 
StringInStringArray(LowerCase(s),
    ['black', 'maroon', 'green', 'olive', 'navy', 'purple', 'teal', 'gray',
    'silver', 'red', 'lime', 'yellow', 'blue', 'fuchsia', 'aqua', 'white'])
or StringIsHex(s);
end;

To know whether a string is a legal real number, use below function. In Delphi check if string is a Real, you can use below function. It is using Delphi TryStrToFloat function.


function StringIsReal(s: string): Boolean;
var
  v: double;
begin
  Result := TryStrToFloat(s, v);
end;




Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.

https://forum.everyething.com/delphi-f39/