網頁

2014/01/14

Trim 全形空白 Delphi & C#

C#:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
///<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;

沒有留言:

張貼留言