您如何在Lazarus中大写字符串中的每个单词

问题描述 投票:-3回答:3

我在这个问题上遇到了很多困难,因为我似乎无法创建一个将每个单词都大写的函数。

一个例子是:

output 'enter sentence'
input'sentence sentence'
output'Sentence Sentence'
freepascal lazarus
3个回答
-1
投票

看起来您只需要使用UpperCase函数:

https://www.freepascal.org/docs-html/rtl/sysutils/uppercase.html


-1
投票

您可以执行以下操作:

function TitleCase(const AString: string): string;
var
  I : Integer;
begin
  Result := '';
  for I := 0 to Length(AString) do begin
    if ((I <= 1) or SameText(Copy(AString, I-1, 1), #32) ) then
       Result := Result + UpperCase(AString[I])
    else Result := Result + LowerCase(AString[I]);
  end;
  Result := Trim(Result);
end;

Writeln(TitleCase('sentence sentence'));

输出

句子句子


-2
投票

也许是矫kill过正,但只需使用正则表达式即可。假设您有两个编辑控件,

uses RegularExpressions;

function TForm1.Evaluate(const Match: TMatch): string;
var
  C1, C2: string;
begin
  C1 := Match.Groups.Item[1].Value;
  C2 := Match.Groups.Item[2].Value;
  Result := UpperCase(C1) + LowerCase(C2);
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  Edit2.Text := TRegEx.Replace(Edit1.Text, '([^\w]\w|^\w)(\w*)', Evaluate);
end;

P.S。此代码需要Delphi XE或更高版本才能支持RegEx。

© www.soinside.com 2019 - 2024. All rights reserved.