Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void Main(string[] args)
- {
- string test = " Hello, World! ";
- Console.WriteLine(Trim(test.ToCharArray()).ToArray());
- }
- private static Span<char> Trim(Span<char> source)
- {
- if (source.IsEmpty)
- {
- return source;
- }
- int start = 0, end = source.Length - 1;
- char startChar = source[start], endChar = source[end];
- while ((start < end) && (startChar == ' ' || endChar == ' '))
- {
- if (startChar == ' ')
- {
- start++;
- }
- if (endChar == ' ')
- {
- end—;
- }
- startChar = source[start];
- endChar = source[end];
- }
- return source.Slice(start, end - start + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement