2014/01/14

Trim 全形空白 Delphi & C#

C#:
using System;

namespace SampleApplication
{
    static class Program
    {
        ///<summary>
        /// Trim (char)12288. Power by EdenW.
        ///</summary>
        public static string TrimEx(string S)
        {
          int I = 0;
          int L = S.Length - 1;
          while ((I <= L) && ((S[I]==' ') || (S[I]==' ')))
            I++;
            
          if (I > L) return "";
          else
          {
            while ((S[L]==' ') || (S[L]<=' '))
              L--;
            
            return S.Substring(I, L - I + 1);
          }
        }    
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Console.WriteLine("Hello world!");
            Console.WriteLine(TrimEx(" Hello world! "));
        }
    }
}


Delphi
///<summary>
/// Trim (char)12288. Power by EdenW.
///</summary>
function TrimEx(const S: string): string;
var
  I, L: Integer;
begin
  L := Length(S);
  I := 1;
  while (I <= L) and ((S[I]=' ') or (S[I]<=' ')) do Inc(I);
  if I > L then Result := '' else
  begin
    while ((S[L]=' ') or (S[L]<=' ')) do Dec(L);
    Result := Copy(S, I, L - I + 1);
  end;
end;



沒有留言:

張貼留言

How to use URLEncode in Delphi

URL編碼,也稱【百分號編碼(Percent-encoding)】 由於 URL 不能直接接受 UTF8 文字,所以要先轉成百分號編碼後,如此網址才能順利送給 HTTP Server 從 Indy 加入 Delphi 後,就可以用 IdURI.TIdURI 單元進行編碼 ...