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;



留言

這個網誌中的熱門文章

對於 Delphi 10.1 Berlin 推出的看法

IntraWeb 學習日記:【Login】