ksString.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. {*******************************************************}
  2. { }
  3. { Methods of Sting }
  4. { }
  5. { CopyRight (C) 2018-2020 KngStr }
  6. { }
  7. {*******************************************************}
  8. unit ksString;
  9. interface
  10. /// <summary>
  11. /// 创建空白文件
  12. /// </summary>
  13. function CreateBlankFile(S: string): Boolean;
  14. /// <summary>
  15. /// 自动为已存在文件名加数字
  16. /// </summary>
  17. function AutoChangeFileName(AFile: string): string;
  18. /// <summary>
  19. /// 自动替换文件名中的非法字符为
  20. /// </summary>
  21. procedure ChangeInvalidFileNameChars(var FileName: string; AChar: Char = '_');
  22. /// <summary>
  23. /// 格式化秒为时间,冒号分割
  24. /// </summary>
  25. function FormatSeconds(const AValue: Int64): string; overload;
  26. /// <summary>
  27. /// 格式化秒为时间,冒号分割
  28. /// </summary>
  29. function FormatSeconds(const AValue: Integer): string; overload;
  30. /// <summary>
  31. /// 格式化秒为时间,冒号分割
  32. /// </summary>
  33. function FormatSeconds(const AValue: Double): string; overload;
  34. /// <summary>
  35. /// 是否中国手机号
  36. /// 13900000000
  37. /// +8613900000000
  38. /// +86 139 0000 0000
  39. /// +86-139-0000-0000
  40. /// </summary>
  41. /// <param name="AOnlyNum">仅允许数字和开头的+</param>
  42. function IsChineseMobileNumber(S: string; AOnlyNum: Boolean = False): Boolean;
  43. implementation
  44. uses
  45. Classes, SysUtils, IOUtils;
  46. function CreateBlankFile(S: string): Boolean;
  47. var
  48. FStrm: TFileStream;
  49. begin
  50. Result := False;
  51. try
  52. FStrm := TFileStream.Create(S, fmCreate);
  53. if FStrm = nil then
  54. Exit;
  55. Result := True;
  56. FreeAndNil(FStrm);
  57. except
  58. FStrm := nil;
  59. end;
  60. end;
  61. function AutoChangeFileName(AFile: string): string;
  62. var
  63. I: Integer;
  64. sPath, sExt: string;
  65. begin
  66. Result := AFile;
  67. I := 0;
  68. sPath := ChangeFileExt(Result, '');
  69. sExt := ExtractFileExt(Result);
  70. while FileExists(Result) do begin
  71. Inc(I);
  72. Result := Format('%s(%d)%s', [sPath, I, sExt]);
  73. end;
  74. end;
  75. procedure ChangeInvalidFileNameChars(var FileName: string; AChar: Char);
  76. var
  77. PFileName: PChar;
  78. FileNameLen: Integer;
  79. Ch: Char;
  80. I: Integer;
  81. begin
  82. I := 0;
  83. PFileName := PChar(FileName);
  84. FileNameLen := Length(FileName);
  85. while I < FileNameLen do begin
  86. Ch := PFileName[I];
  87. if not TPath.IsValidFileNameChar(Ch) then
  88. PFileName[I] := AChar
  89. else
  90. Inc(I);
  91. end;
  92. end;
  93. function FormatSeconds(const AValue: Double): string; overload;
  94. var
  95. h, m, s, ms: Integer;
  96. begin
  97. ms := Trunc(Frac(AValue) * 1000);
  98. s := Trunc(AValue);
  99. m := s div 60;
  100. h := m div 60;
  101. if h > 0 then
  102. Result := Format('%.2d:%.2d:%.2d.%.3d', [h, m mod 60, s mod 60, ms])
  103. else
  104. Result := Format('%.2d:%.2d.%.3d', [m, s mod 60, ms]);
  105. end;
  106. function FormatSeconds(const AValue: Int64): string;
  107. var
  108. h, m, s: Integer;
  109. begin
  110. s := AValue;
  111. m := s div 60;
  112. h := m div 60;
  113. if h > 0 then
  114. Result := Format('%.2d:%.2d:%.2d', [h, m mod 60, s mod 60])
  115. else
  116. Result := Format('%.2d:%.2d', [m, s mod 60]);
  117. end;
  118. function FormatSeconds(const AValue: Integer): string; overload;
  119. begin
  120. FormatSeconds(Int64(AValue));
  121. end;
  122. function IsChineseMobileNumber(S: string; AOnlyNum: Boolean): boolean;
  123. var
  124. p: PChar;
  125. i: Integer;
  126. begin
  127. Result := False;
  128. // 最短肯定是11位
  129. if (Length(S) < 11) then
  130. Exit;
  131. i := 0;
  132. p := PChar(S);
  133. while p^ <> #0 do begin
  134. if p^ = '+' then begin
  135. if i > 0 then // +号必须在第一位
  136. Exit;
  137. if (p[1] = '8') and (p[2] = '6') then // +86,而且是连续的
  138. Inc(p, 3)
  139. else
  140. Exit;
  141. end
  142. else if ((p^ >= '0') and (p^ <= '9')) then begin
  143. if (i = 0) and (p^ <> '1') then // 中国手机号都是1开头
  144. Exit;
  145. Inc(p);
  146. Inc(i);
  147. end
  148. else if (not AOnlyNum) and ((p^ = '-') or (p^ = ' ')) then
  149. Inc(p)
  150. else
  151. Exit;
  152. end;
  153. Result := i = 11; //中国手机号 11位
  154. end;
  155. end.