function GetSolidStr(const S: string): string;
var
   Len: Byte;
   FirstPos, LastPos: Byte;
begin
   Result := '';
   if S = '' then Exit;   Len := Length(S);
   FirstPos := 1;
   while (FirstPos <= Len) and
         (S[FirstPos] <= #32) do begin
      Inc(FirstPos);
   end;
   if FirstPos > Len then Exit;   for LastPos := Len downto FirstPos do begin
     if S[FirstPos] > #32 then
        Break;
   end;   Result := Copy ( S, FirstPos, LastPos - FirstPos +1 );
end;

解决方案 »

  1.   

    #32 等价于 chr(32),就是取32对应的ascii码
      

  2.   

    这段代码的意思是取出第一个>=#32(>=空格)和最后一个>=#32直接的字符串
      

  3.   

    这段代码的意思是取出第一个>=#32(>=空格)的字符和最后一个>=#32的字符之间的字符串
      

  4.   

    这个函数主要是用于下面这个转换,我是初学者,还是不懂为什么这么用,xiaocai800322(走自己的路)还在么,再帮我解释一下好么?谢谢!
    EditBIRTHDATE.Text := GetSolidStr(EditBIRTHDATE.Text);
       if (not(Length(EditBIRTHDATE.Text)in [8..10]) or ( Pos('-', EditBIRTHDATE.Text) <> 5 )) then
       begin
          Application.MessageBox('请您输入4位年号,年号、月份和日期用‘-’隔开。', '输入',MB_OK);
          EditBIRTHDATE.SetFocus;
          EditBIRTHDATE.SelectAll;
          exit;
       end
      

  5.   

    你的代码中默认日期为2007-01-01这种样子或者2007-1-1这种样子,你的代码认为日期长度必须为8,9或者10并且第五个字符应该是‘-’,pos(串1,串2)返回串1在串2中的位置。如在你的代码中认为如果日期格式正确Pos('-', EditBIRTHDATE.Text)返回值应该是5,比如pos('-','2007-1-1')或者pos('-','2007-01-01')返回值都是5
      

  6.   

    //如果日期字符串长度不是8..10,或者他的第五个字符不是5 
    if (not(Length(EditBIRTHDATE.Text)in [8..10]) or ( Pos('-', EditBIRTHDATE.Text) <> 5 )) then
       begin
          //提示正确的输入格式
          Application.MessageBox('请您输入4位年号,年号、月份和日期用‘-’隔开。', '输入',MB_OK);
          //光标定位到EditBIRTHDATE文本框
          EditBIRTHDATE.SetFocus;
          //EditBIRTHDATE文本框中的所有文字被选中,应该呈蓝色
          EditBIRTHDATE.SelectAll;
          exit;
       end
      

  7.   

    xiaocai800322(走自己的路),谢谢你啊,现在知道了,但是这个函数还是不太清楚,是不是去掉输入内容两边的空格阿。function GetSolidStr(const S: string): string;
      

  8.   

    function GetSolidStr(const S: string): string;
    这个函数我也不太清楚,以前没有用过,我觉得这应该是你的程序里面的自己写的函数
    据猜应该是去掉字符串中所有空格的函数,我去掉两边空格的时候一般用trim(引用sysUtils单元)
      

  9.   

    这个就是自定义的函数,谢谢你xiaocai800322,让我少走了很多弯路,我是初学者,有很多东西都不懂。
      

  10.   

    for LastPos := Len downto FirstPos do begin
         if S[FirstPos] > #32 then//错了吧  if S[LastPos ] > #32 
            Break;
       end;取char(32) 中间的字符串;
      

  11.   

    原函数中有处错误:
      for LastPos := Len downto FirstPos do begin
         if S[FirstPos] > #32 then
    应该是
      for LastPos := Len downto FirstPos do begin
         if S[LastPos] > #32 then另外, 这个函数可以简化一下:
    function GetSolidStr(const S: string): string;
    var
      FirstPos, LastPos: Integer;
    begin
      Result := '';
      if S = '' then exit;
      for FirstPos := 1 to Length(S) do
        if S[FirstPos] > #32 then
        begin
          for LastPos := Length(S) downto FirstPos do
            if S[LastPos] > #32 then
            begin
              Result := Copy(S, FirstPos, LastPos - FirstPos + 1);
              break;
            end;
          break;
        end;
    end;
      

  12.   

    就是判断输入的s的每一字符的asicc码,是不是小与32
    找一下asicc码表,要不我找一下?