unit U_CommFunc;


interface

uses
  Windows, Messages,dialogs, SysUtils, Variants,Classes,
   StdCtrls, ExtCtrls;

  function split(STR_Source :string; STR_Split:string):TStringList; //分割字符串
  function AddNewLineToStr(sourceStr:String;number:integer):String ; //在字符串中加入换行符号
  function StrToE(source:string;ID:integer;flg:integer):String;   //加密函数
  function splitChnFromStr(sourceStr:String):TStringList;  //从字符串中分离出计算变量
  function strToArray(sourceStr:string;number:integer):TStringList; //将中文字符串按没行指定的个数分割
  function okNumeric(str:string):boolean; //验证输入的是否是数字
  function okNan(str:string):boolean;
  function IsNumber(mStr: string): Boolean;   //验证数字 
  function IsInteger(mStr: string): Boolean;  //验证整型
  //把字符串转化为一行一个
  function StrToOne(sourceStr:string):String;
implementation

//字符串分割函数
function split(STR_Source :string; STR_Split:string):TStringList;
var
	temp:String;
	i:Integer;
begin
	Result:=TStringList.Create;
	//如果是空自符串则返回空列表
	if trim(STR_Source) = '' then exit;
	temp:=STR_Source;
	i:=pos(STR_Split,STR_Source);
	while i <> 0 do
	begin
		Result.add(copy(temp,0,i-1));
		Delete(temp,1,i+length(STR_Split)-1); //如果STR_Split长度大于1的话,原来的只删除STR_Split字符的第一个.
		i:=pos(STR_Split,temp);
	end;
	Result.add(temp);
end;
//**************************************************************
  //在字符串中加入换行符号      number:一行字符串的个数
function AddNewLineToStr(sourceStr:String;number:integer):String ;
var
  i:integer;
  tmpStr:string;
  tstr_tmp:TstringList;
  isNext:boolean;
begin
  isNext:=false;
  tmpStr:='';
  tstr_tmp:=Tstringlist.create();
  for i:=1 to length(sourceStr) do
    begin
      if isNext then
        begin
          isNext:=false ;
          continue;
        end;
      if (ord(sourceStr[i])>=33)and(ord(sourceStr[i])<=126) then
        begin
          tstr_tmp.Append(sourceStr[i]);
          isNext:=false;
        end
      else
        if (ord(sourceStr[i])>=127) then
          begin
            tstr_tmp.Append(copy(sourceStr,i,2));
            isNext:=true;
          end;
  end; //end for

  for i:=1 to tstr_tmp.Count do
    begin
       if (i mod number=0) and (i<>tstr_tmp.Count) then
          tmpStr:=tmpStr+tstr_tmp.Strings[i-1]+chr(13)
       else
         tmpStr:=tmpStr+tstr_tmp.Strings[i-1];
    end;

  result:=tmpStr;
end;

//**************************************************************
  // 从字符串中分离出计算变量
function splitChnFromStr(sourceStr:String):TStringList ;
var
  i:integer;
  tstr_tmp:TstringList;
  isNext:boolean;
  tmp:String;
begin
  isNext:=false;
  tstr_tmp:=Tstringlist.create();
  for i:=1 to length(sourceStr) do
    begin
      if isNext then
        begin
          isNext:=false ;
          continue;
        end;
      if (ord(sourceStr[i])>=33)and(ord(sourceStr[i])<=126) then
        begin
          if sourceStr[i] in['/','=','-','+','*','\','(',')'] then
             begin
               if Length(tmp)>2 then
                  begin
                    tstr_tmp.Append(tmp);
                    tmp:='';
                  end;
               isNext:=false;
             end
          else
            begin
              tmp:=tmp+sourceStr[i];
              isNext:=false;
            end;
        end
      else
        if (ord(sourceStr[i])>=127) then
          begin
            //tstr_tmp.Append(copy(sourceStr,i,2));
            tmp:=tmp+copy(sourceStr,i,2);
            isNext:=true;
          end;
  end; //end for
  if Length(tmp)>2 then  tstr_tmp.Append(tmp);
  result:=tstr_tmp;
end;

//*****************************************************
  // 加密  1:加  0:减
function StrToE(source:string;ID:integer;flg:integer):String;
var
  i:integer;
  tmp:String;
begin
  setLength(tmp,length(source));
  if flg=1 then
     begin
       for i:=1 to Length(source) do
         begin
           tmp[i]:=chr(ord(source[i])+ID);
         end;
     end
  else if flg=0 then
    begin
      for i:=1 to Length(source) do
         begin
            tmp[i]:=chr(ord(source[i])-ID);
         end;
    end;
  result:=tmp;
end;
//********************************************************************
 //将中文字符串按没行指定的个数分割
function strToArray(sourceStr:string;number:integer):TStringList;
var
  i,cnt:integer;
  tmpStr:string;
  tstr_tmp1,tstr_tmp2:TstringList;
  isNext:boolean;
begin
  isNext:=false;
  tmpStr:='';
  tstr_tmp1:=Tstringlist.create();
  tstr_tmp2:=Tstringlist.create();
  for i:=1 to length(sourceStr) do
    begin
      if isNext then
        begin
          isNext:=false ;
          continue;
        end;
      if (ord(sourceStr[i])>=33)and(ord(sourceStr[i])<=126) then
        begin
          tstr_tmp1.Append(sourceStr[i]);
          isNext:=false;
        end
      else
        if (ord(sourceStr[i])>=127) then
          begin
            tstr_tmp1.Append(copy(sourceStr,i,2));
            isNext:=true;
          end;
  end; //end for

  if number=1 then
    begin
      result:=tstr_tmp1;
      exit;
    end;
  cnt:=1;
  tmpStr:='';
  for i:=0 to tstr_tmp1.Count-1 do
    begin
      if cnt<=number then
        begin
          tmpStr:=tmpStr+tstr_tmp1.strings[i];
          inc(cnt);
        end
      else
        begin
          tstr_tmp2.append(tmpStr);
          tmpStr:=tstr_tmp1.strings[i];
          cnt:=2;
        end;
    end;
  if tmpStr<>'' then  tstr_tmp2.append(tmpStr);
  result:=tstr_tmp2;
end;

//*************************************************************
  //验证输入的是否是数字
function okNumeric(str:string):boolean;
begin
  str:=trim(str);
  try
    if str='' then
      begin
        result:=false;
        exit;
      end;
    strToFloat(str);
    result:=true;
  except
    on EConvertError do
    result:=false;
  end;
end;
//*************************************************************
  //验证输入的是否是数字
function okNan(str:string):boolean;
begin
  result:=true;
  str:=trim(str);
  try
    if str='' then
      begin
        result:=false;
        exit;
      end;
    strToInt(str);
  except
    on EConvertError do
    result:=false;
  end;
end;
//****************************************************************
  //把字符串转化为一行一个
function StrToOne(sourceStr:string):String;
var
  i:integer;
  isNext:Boolean;
  tmp:string;
begin
  isNext:=false;
  for i:=1 to length(sourceStr) do
  begin
      if isNext then
        begin
          isNext:=false ;
          continue;
        end;
      if (ord(sourceStr[i])>=33)and(ord(sourceStr[i])<=126) then
        begin
          tmp:=tmp+(sourceStr[i]);
          isNext:=false;
        end
      else
        if (ord(sourceStr[i])>=127) then
          begin
             tmp:=tmp+(copy(sourceStr,i,2))+#13;
             isNext:=true;
          end;
  end; //end for
  result:=tmp;
end;
//* 返回字符串是否是正确的数字表达 *//
function IsNumber(mStr: string): Boolean;
var
  I: Real;
  E: Integer;
begin
  Val(mStr, I, E);
  Result := E = 0;
  E := Trunc(I);
end;
// * 返回字符串是否是正确的整数表达  *//
function IsInteger(mStr: string): Boolean;
var
  I: Integer;
  E: Integer;
begin
  Val(mStr, I, E);
  Result := E = 0;
  E := Trunc(I);
end;

end.


