Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a character from a string is upper or lower case?

I'm expanding a class of mine for storing generic size strings to allow more flexible values for user input. For example, my prior version of this class was strict and allowed only the format of 2x3 or 9x12. But now I'm making it so it can support values such as 2 x 3 or 9 X 12 and automatically maintain the original user's formatting if the values get changed.

The real question I'm trying to figure out is just how to detect if one character from a string is either upper or lower case? Because I have to detect case sensitivity. If the deliminator is 'x' (lowercase) and the user inputs 'X' (uppercase) inside the value, and case sensitivity is turned off, I need to be able to find the opposite-case as well.

I mean, the Pos() function is case sensitive...

like image 595
Jerry Dodge Avatar asked Nov 29 '22 03:11

Jerry Dodge


2 Answers

Delphi 7 has UpperCase() and LowerCase() functions for strings. There's also UpCase() for characters.

If I want to search for a substring within another string case insensitively, I do this:

if Pos('needle', LowerCase(hayStack)) > 0 then

You simply use lower case string literals (or constants) and apply the lowercase function on the string before the search. If you'll be doing a lot of searches, it makes sense to convert just once into a temp variable.

Here's your case:

a := '2 x 3';  // Lowercase x
b := '9 X 12'; // Upper case X

x := Pos('x', LowerCase(a)); // x = 3
x := Pos('x', LowerCase(b)); // x = 3

To see if a character is upper or lower, simply compare it against the UpCase version of it:

a := 'A';
b := 'b';

upper := a = UpCase(a); // True
upper := b = UpCase(b); // False
like image 52
Marcus Adams Avatar answered Dec 09 '22 16:12

Marcus Adams


try using these functions (which are part of the Character unit)

  • Character.TCharacter.IsUpper
  • Character.TCharacter.IsLower
  • IsLower
  • IsUpper

UPDATE

For ansi versions of delphi you can use the GetStringTypeEx functions to fill a list with each ansi character type information. and thne compare the result of each element against the $0001(Upper Case) or $0002(Lower Case) values.

uses
  Windows,
  SysUtils;

Var
  LAnsiChars: array [AnsiChar] of Word;

procedure FillCharList;
var
  lpSrcStr: AnsiChar;
  lpCharType: Word;
begin
  for lpSrcStr := Low(AnsiChar) to High(AnsiChar) do
  begin
    lpCharType := 0;
    GetStringTypeExA(LOCALE_USER_DEFAULT, CT_CTYPE1, @lpSrcStr, SizeOf(lpSrcStr), lpCharType);
    LAnsiChars[lpSrcStr] := lpCharType;
  end;
end;

function CharIsLower(const C: AnsiChar): Boolean;
const
  C1_LOWER  = $0002;
begin
  Result := (LAnsiChars[C] and C1_LOWER) <> 0;
end;

function CharIsUpper(const C: AnsiChar): Boolean;
const
  C1_UPPER  = $0001;
begin
  Result := (LAnsiChars[C] and C1_UPPER) <> 0;
end;

begin
  try
    FillCharList;
    Writeln(CharIsUpper('a'));
    Writeln(CharIsUpper('A'));
    Writeln(CharIsLower('a'));
    Writeln(CharIsLower('A'));
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
  Readln;
end.
like image 36
RRUZ Avatar answered Dec 09 '22 14:12

RRUZ