/// <summary> /// <paramref name="Value"/>의 문자열을 특정 길이만큼 잘라서 배열을 만듦. /// </summary> /// <param name="Value">대상 문자열</param> /// <param name="Length">자를 길이</param> /// <param name="IsTwoByte">2바이트 문자열의 길이를 2로 취급할 지 여부</param> /// <returns>각 항목이 특정 길이를 넘지 않는 배열</returns> /// <example> /// <code> /// string Value = "a01나03"; /// /// string[] aValue1 = CArray.SplitByLength(Value, 3, false); /// string[] aValue2 = CArray.SplitByLength(Value, 3, true); /// /// Console.WriteLine(string.Join(",", aValue1)); //a01,나03 /// Console.WriteLine(string.Join(",", aValue2)); //a01,나0,3 /// </code> /// </example> public static string[] SplitByLength(string Value, int Length, bool IsTwoByte) { List <string> aValue = new List <string>(); bool IsEnd = false; //처음엔 (Value != "") 로 검사했으니 마지막에 ModIs가 1이 되면 무한 루프 //발생하여 IsEnd로 체크함. int LenCur = 0; if (!IsTwoByte) { while (!IsEnd) { LenCur = Value.Length; if (LenCur > Length) { LenCur = Length; } else { IsEnd = true; } aValue.Add(Value.Substring(0, LenCur)); Value = Value.Substring(LenCur); } } else { while (!IsEnd) { LenCur = CTwoByte.LenH(Value); if (LenCur > Length) { LenCur = Length; } else { IsEnd = true; } int ModIs; aValue.Add(CTwoByte.SubstringH(Value, 0, LenCur, out ModIs)); Value = CTwoByte.SubstringH(Value, (LenCur - ModIs)); } } //return (string[])aValue.ToArray(typeof(System.String)); return(aValue.ToArray()); }
/// <example> /// Console.WriteLine(RowSource.PadLeftH("a한b", 2, 'x')); //xa /// Console.WriteLine(RowSource.PadLeftH("한글", 2, 'x')); //한 /// </example> public static string PadLeftH(string Value, int TotalWidth, char PaddingChar) { int LenH = CTwoByte.LenH(Value); if (LenH > TotalWidth) { int ModIs; Value = CTwoByte.LeftH(Value, TotalWidth, out ModIs); LenH = CTwoByte.LenH(Value); } int nPad = TotalWidth - LenH; return(CFindRep.Repeat(PaddingChar, nPad) + Value); }
/// <summary> /// 가운데 정렬 후 양쪽에 <paramref name="PaddingChar"/>를 채워서 <paramref name="TotalWidth"/> 길이를 맞춤. /// </summary> /// <param name="Value"></param> /// <param name="TotalWidth"></param> /// <param name="PaddingChar"></param> /// <returns></returns> /// <example> /// string s = CTwoByte.PadCenterH("안a녕", 8, 'x'); //"x안a녕xx" /// string s2 = CTwoByte.PadCenterH("12", 1, 'x'); //"12" /// string s3 = CTwoByte.PadCenterH("12", 3, 'x'); //"12x" /// </example> public static string PadCenterH(string Value, int TotalWidth, char PaddingChar) { int LenH = CTwoByte.LenH(Value); if (TotalWidth <= LenH) { return(Value); } int Half = (int)((TotalWidth - LenH) / 2); int Mod = (TotalWidth - LenH) % 2; if (Half == 0) { return(Value + CFindRep.Repeat(PaddingChar, Mod)); } string Pad = CFindRep.Repeat(PaddingChar, Half); return(Pad + Value + Pad + CFindRep.Repeat(PaddingChar, Mod)); }
/// <summary> /// 다음 규칙에 맞는 유효한 아이디인 지 확인함. /// 길이는 MinLength부터 MaxLength까지, 영어와 숫자만 가능, 첫자는 영어만 가능. /// </summary> /// <param name="Id"></param> /// <param name="MinLength"></param> /// <param name="MaxLength"></param> /// <param name="IsAllowHangul"></param> /// <param name="ErrMsgIs"></param> /// <returns></returns> public static bool IsId(string Id, int MinLength, int MaxLength, bool IsAllowHangulHanja, out string ErrMsgIs) { ErrMsgIs = ""; const string AlphaList = "abcdefghijklmnopqrstuvwxyz"; const string NumList = "1234567890"; const string AlphaNumList = AlphaList + NumList; if (string.IsNullOrEmpty(Id)) { ErrMsgIs = "아이디를 입력하지 않았습니다."; return(false); } int Len = CTwoByte.LenH(Id); if ((Len < MinLength) || (Len > MaxLength)) { ErrMsgIs = "아이디는 " + MinLength + " ~ " + MaxLength + "자 사이만 가능합니다"; return(false); } char FirstChar = Id[0]; if (IsAllowHangulHanja) { bool IsHangul = CValid.IsHangul(FirstChar); bool IsHanja = CValid.IsHanja(FirstChar); bool IsAlpha = (AlphaList.IndexOf(FirstChar) != -1); if (!IsHangul && !IsHanja && !IsAlpha) { ErrMsgIs = "아이디의 첫번째는 한글, 한자, 영문만 가능합니다."; return(false); } for (int i = 0, i2 = Id.Length; i < i2; i++) { char c = Id[i]; bool IsHangulCur = CValid.IsHangul(c); bool IsHanjaCur = CValid.IsHanja(c); bool IsAlphaOrNumCur = (AlphaNumList.IndexOf(c) != -1); if (!IsHangulCur && !IsHanja && !IsAlphaOrNumCur) { ErrMsgIs = "아이디는 한글, 한자, 영문, 숫자만 가능합니다."; return(false); } } } else { if (AlphaList.IndexOf(FirstChar) == -1) { ErrMsgIs = "아이디의 첫번째는 영문만 가능합니다"; return(false); } for (int i = 0, i2 = Id.Length; i < i2; i++) { char c = Id[i]; bool IsAlphaOrNumCur = (AlphaNumList.IndexOf(c) != -1); if (!IsAlphaOrNumCur) { ErrMsgIs = "아이디는 영문, 숫자만 가능합니다"; return(false); } } } switch (Id) { case "sys": case "system": case "admin": case "administrator": case "master": case "webmaster": case "developer": case "dev": case "helper": case "관리자": case "운영자": case "개발자": case "도우미": ErrMsgIs = Id + "는 시스템에서 사용되므로 허용되지 않는 아이디입니다."; return(false); } return(true); }