Common.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Microsoft.AspNetCore.Mvc;
  2. using System.Text.RegularExpressions;
  3. using System.Xml.Serialization;
  4. namespace economy.Helpers
  5. {
  6. public class Common
  7. {
  8. // XML 데이터 직렬화
  9. public static async Task<T> ParseXmlDataAsync<T>(string xmlData) where T : class
  10. {
  11. var serializer = new XmlSerializer(typeof(T));
  12. using (var reader = new StringReader(xmlData))
  13. {
  14. return await Task.Run(() => (T)serializer.Deserialize(reader));
  15. }
  16. }
  17. // 시작 번호 조회
  18. public static int CalcListNumber(int total, int page, int perPage)
  19. {
  20. return (total - ((page - 1) * perPage));
  21. }
  22. // 천 단위 구분 형식으로 변환
  23. public static string NumberFormat(string s, string format = "N0")
  24. {
  25. if (int.TryParse(s, out int clprValue))
  26. {
  27. s = clprValue.ToString(format);
  28. }
  29. if (double.TryParse(s, out double doubleValue))
  30. {
  31. return doubleValue.ToString(format);
  32. }
  33. if (decimal.TryParse(s, out decimal decimalValue))
  34. {
  35. return decimalValue.ToString(format);
  36. }
  37. return s;
  38. }
  39. // 문자열 형태의 날짜를 포맷 형식으로 변환
  40. public static string StringToDateFormat(string s, string format = "yyyy-MM-dd")
  41. {
  42. return DateTime.ParseExact(s, "yyyyMMdd", null).ToString(format);
  43. }
  44. // 도메인 형식 여부
  45. public static bool IsDomain(string input)
  46. {
  47. return Regex.IsMatch(input, @"^[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}$");
  48. }
  49. // IPv4 형식 여부
  50. public static bool IsIPv4(string input)
  51. {
  52. return Regex.IsMatch(input, @"^(\d{1,3}\.){3}\d{1,3}$");
  53. }
  54. // IPv6 형식 여부
  55. public static bool IsIPv6(string input)
  56. {
  57. return Regex.IsMatch(input, @"^([0-9a-fA-F]{1,4}:){1,7}[0-9a-fA-F]{1,4}(/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))?$");
  58. }
  59. }
  60. }