简体   繁体   中英

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...

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

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

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.
   if myChar in ['A'..'Z'] then
    begin
      // uppercase
    end
   else
      if myChar in ['a'..'z'] then
      begin
        // lowercase
      end
      else
        begin
          // not an alpha char
        end;

..or D2009 on..

   if charInSet(myChar,['A'..'Z']) then
   begin
      // uppercase
   end
   else
     if charInSet(myChar,['a'..'z']) then
     begin
       // lowercase
     end
     else
        begin
          // not an alpha char
        end;

The JCL has routines for this in the JclStrings unit, eg CharIsUpper and CharIsLower. SHould work in Delphi 7.

AnsiPos() is not case-sensitive. You can also force upper or lower case, irrespective of what the user enters using UpperCase() and LowerCase() .

Just throwing this out there since you may find it far more simple than the other (very good) answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM