TrimString() 말줄임 메서드 추가

This commit is contained in:
iyak 2025-04-04 04:36:52 +00:00
parent f9e0f5dcdc
commit 13f77b2859
1 changed files with 30 additions and 0 deletions

View File

@ -601,4 +601,34 @@ public static class Helpers
return false;
}
/// <summary>
/// 문자열을 지정된 최대 자리수만큼 자르고 초과하면 "..."을 추가.
/// </summary>
/// <param name="text">문자열</param>
/// <param name="maxLength">최대 자리수</param>
/// <returns>잘린 문자열</returns>
public static string TrimString(string text, int maxLength)
{
// 유효성 검사
if (string.IsNullOrEmpty(text) || maxLength <= 0)
{
return string.Empty;
}
// "..."도 자리수로 포함되므로 자리수가 3 이하라면 "..."을 바로 반환
if (maxLength <= 3)
{
return "...";
}
// 문자열이 최대 자리수를 초과하지 않는다면 그대로 반환
if (text.Length <= maxLength)
{
return text;
}
// 문자열 자르기 + "..."
return text.Substring(0, maxLength - 3) + "...";
}
}