是否有任何函数/过程作为ReplaceString,但对于Delphi上的整个单词?

时间:2014-09-15 16:12:56

标签: string delphi

是否有任何函数/过程ReplaceString但是对于Delphi上的整个单词?我只需要将子串替换为新的子串,当它是一个完整的单词时。例如:

substring - > newstring
substring, -> newstring
substring123 -> substring

1 个答案:

答案 0 :(得分:5)

您可以使用内置的正则表达式库来执行此操作。例如:

{$APPTYPE CONSOLE}

uses
  System.RegularExpressions;

const
  InputString = 'a Substring, substring123, some more text';

begin
  Writeln(TRegEx.Replace(InputString, '\bsubstring\b', 'newstring', [roIgnoreCase]));
end.