Skip to content

Commit f627bd6

Browse files
committed
tryget string
1 parent 1f3e498 commit f627bd6

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

CSUtilities/Extensions/StringExtensions.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static bool IsNullOrWhiteSpace(this string str)
4545
}
4646

4747
/// <summary>
48-
///
48+
///
4949
/// </summary>
5050
/// <param name="str"></param>
5151
/// <exception cref="ArgumentException"></exception>
@@ -55,7 +55,7 @@ public static void TrowIfNullOrEmpty(this string str)
5555
}
5656

5757
/// <summary>
58-
///
58+
///
5959
/// </summary>
6060
/// <param name="str"></param>
6161
/// <param name="message"></param>
@@ -235,15 +235,14 @@ public static string ReadUntil(this string str, char c, out string residual)
235235
}
236236

237237
return value;
238-
239238
}
240239

241240
/// <summary>
242241
/// Remove all the first whitespaces in a string.
243242
/// </summary>
244243
/// <param name="str"></param>
245244
/// <returns></returns>
246-
public static string RemoveStartWhitespaces(this string str)
245+
public static string RemoveStartWhiteSpaces(this string str)
247246
{
248247
while (str.StartsWith(" "))
249248
{
@@ -375,6 +374,22 @@ public static string[] ToArgs(this string str, char separator, char stringDelimi
375374
return args.ToArray();
376375
}
377376

377+
/// <summary>
378+
/// Attempts to retrieve the character at the specified index in the string.
379+
/// </summary>
380+
/// <param name="str">The string from which to retrieve the character. Cannot be <see langword="null"/>.</param>
381+
/// <param name="index">The zero-based index of the character to retrieve.</param>
382+
/// <returns>The character at the specified index, or <see langword="null"/> if the index is out of range.</returns>
383+
public static char? TryGet(this string str , int index)
384+
{
385+
if(index < 0 || index >= str.Length)
386+
{
387+
return null;
388+
}
389+
390+
return str[index];
391+
}
392+
378393
public static string[] ToArgs(this string str, char separator, IDictionary<char, char> groupDelimitiers, bool keepTokens = false)
379394
{
380395
List<string> args = new List<string>();
@@ -422,4 +437,4 @@ public static string SubstringByIndex(this string s, int startIndex, int endInde
422437
return s.Remove(endIndex).Substring(startIndex);
423438
}
424439
}
425-
}
440+
}

0 commit comments

Comments
 (0)