2014年1月23日 星期四

字串去空白

去前後的空白可使用 trim() 這個函數
要去掉中間的空白,就要使用 
function StringReplace ( const SourceString, OldPattern, NewPattern : string; Flags : TReplaceFlags ) : string

範例:把a改成the
var
  before, after : string;

begin
  // Try to replace all occurrences of a or A to THE
  before := 'This is a way to live A big life';

  after  := StringReplace(before, ' a ', ' THE ',
                          [rfReplaceAll, rfIgnoreCase]);
  ShowMessage('Before = '+before);
  ShowMessage('After  = '+after);
end;

  

1 則留言:

  1. 也可以用正則表示法來去空白..

    System.RegularExpressions
    var regex:Tregex;
    str:='dsf dsaf sdf sdaf ads as s';
    RegEx := TRegEx.Create('\s+'); //使用正則表示法去空白跟換行符號
    str:=Regex.Replace(str, '');
    showmessage(str);

    回覆刪除