|
1 |
| -namespace Thinktecture; |
2 |
| - |
3 |
| -/// <summary> |
4 |
| -/// Extensions methods for <see cref="String"/>. |
5 |
| -/// </summary> |
6 |
| -public static class StringExtensions |
7 |
| -{ |
8 |
| - /// <summary> |
9 |
| - /// If the <paramref name="text"/> is <c>null</c>, an empty <see cref="String"/> or contains whitespaces only, then <c>null</c> is returned. |
10 |
| - /// Otherwise, the <paramref name="text"/> is trimmed and shortened according to <paramref name="maxLength"/>. |
11 |
| - /// </summary> |
12 |
| - /// <param name="text">Text to trim or nullify.</param> |
13 |
| - /// <param name="maxLength">The <paramref name="text"/> is shortened if its length exceeds the provided <paramref name="maxLength"/>.</param> |
14 |
| - /// <returns> |
15 |
| - /// Trimmed and shortened (according to <paramref name="maxLength"/>) <paramref name="text"/>, if the <paramref name="text"/> is not <c>null</c> nor contains whitespaces only; |
16 |
| - /// otherwise <c>null</c>. |
17 |
| - /// </returns> |
18 |
| - /// <exception cref="ArgumentException"> |
19 |
| - /// <paramref name="maxLength"/> is less or equals to 0. |
20 |
| - /// </exception> |
21 |
| - public static string? TrimOrNullify(this string? text, int? maxLength = null) |
22 |
| - { |
23 |
| - if (String.IsNullOrWhiteSpace(text)) |
24 |
| - return null; |
25 |
| - |
26 |
| - text = text.Trim(); |
27 |
| - |
28 |
| - if (maxLength.HasValue) |
29 |
| - { |
30 |
| - if (maxLength <= 0) |
31 |
| - throw new ArgumentException("The maximum length must be bigger than 0.", nameof(maxLength)); |
32 |
| - |
33 |
| - if (text.Length > maxLength) |
34 |
| - text = text[..maxLength.Value]; |
35 |
| - } |
36 |
| - |
37 |
| - return text; |
38 |
| - } |
39 |
| -} |
| 1 | +using System.Runtime.CompilerServices; |
| 2 | + |
| 3 | +namespace Thinktecture; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Extensions methods for <see cref="String"/>. |
| 7 | +/// </summary> |
| 8 | +public static class StringExtensions |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// If the <paramref name="text"/> is <c>null</c>, an empty <see cref="String"/> or contains whitespaces only, then <c>null</c> is returned. |
| 12 | + /// Otherwise, the <paramref name="text"/> is trimmed. |
| 13 | + /// </summary> |
| 14 | + /// <param name="text">Text to trim or nullify.</param> |
| 15 | + /// <returns> |
| 16 | + /// Trimmed <paramref name="text"/>, if the <paramref name="text"/> is not <c>null</c> nor contains whitespaces only; |
| 17 | + /// otherwise <c>null</c>. |
| 18 | + /// </returns> |
| 19 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 20 | + public static string? TrimOrNullify(this string? text) |
| 21 | + { |
| 22 | + return String.IsNullOrWhiteSpace(text) ? null : text.Trim(); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// If the <paramref name="text"/> is <c>null</c>, an empty <see cref="String"/> or contains whitespaces only, then <c>null</c> is returned. |
| 27 | + /// Otherwise, the <paramref name="text"/> is trimmed and shortened according to <paramref name="maxLength"/>. |
| 28 | + /// </summary> |
| 29 | + /// <param name="text">Text to trim or nullify.</param> |
| 30 | + /// <param name="maxLength">The <paramref name="text"/> is shortened if its length exceeds the provided <paramref name="maxLength"/>.</param> |
| 31 | + /// <returns> |
| 32 | + /// Trimmed and shortened (according to <paramref name="maxLength"/>) <paramref name="text"/>, if the <paramref name="text"/> is not <c>null</c> nor contains whitespaces only; |
| 33 | + /// otherwise <c>null</c>. |
| 34 | + /// </returns> |
| 35 | + /// <exception cref="ArgumentException"> |
| 36 | + /// <paramref name="maxLength"/> is less or equals to 0. |
| 37 | + /// </exception> |
| 38 | + public static string? TrimOrNullify(this string? text, int maxLength) |
| 39 | + { |
| 40 | + if (maxLength <= 0) |
| 41 | + throw new ArgumentException("The maximum length must be bigger than 0.", nameof(maxLength)); |
| 42 | + |
| 43 | + if (String.IsNullOrWhiteSpace(text)) |
| 44 | + return null; |
| 45 | + |
| 46 | + text = text.Trim(); |
| 47 | + |
| 48 | + if (text.Length > maxLength) |
| 49 | + text = text[..maxLength]; |
| 50 | + |
| 51 | + return text; |
| 52 | + } |
| 53 | +} |
0 commit comments