2004. december 28., kedd

How to check for any lower case or space in a string


Problem/Question/Abstract:

How to check for any lower case or space in a string

Answer:

function ContainsLowerCaseOrSpace(AString: string): boolean;
var
  MySet: set of char;
  Len, Counter: integer;
begin
  MySet := ['a'..'z', ' '];
  Len := Length(AString);
  Result := Len <> 0;
  Counter := 1;
  while (not Result) and (Counter <= Len) do
  begin
    if AString[Counter] in MySet then
      Result := True
    else
      Inc(Counter);
  end;
end;

procedure AddSpaceBeforeUpperCaseCharOrNumber(var AString: string);
var
  Counter: integer;
  bLastIsNumber: boolean;
  bLastIsUpper: boolean;
begin
  Counter := Length(AString);
  bLastIsUpper := False; {Assume the last character will never be an upper case}
  bLastIsNumber := AString[Counter] in ['0'..'9'];
  dec(Counter);
  while Counter > 1 do
  begin
    if AString[Counter] in ['0'..'9'] then
    begin
      if not bLastIsNumber then
        Insert(' ', AString, Counter + 1);
      bLastIsNumber := True;
    end
    else
    begin
      if bLastIsNumber or bLastIsUpper then
      begin
        Insert(' ', AString, Counter + 1);
        bLastIsNumber := False;
      end;
      bLastIsUpper := AString[Counter] in ['A'..'Z'];
    end;
    dec(Counter);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése