Advertisement
Guest User

Untitled

a guest
May 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. private static void Main(string[] args)
  2. {
  3. string test = " Hello, World! ";
  4. Console.WriteLine(Trim(test.ToCharArray()).ToArray());
  5. }
  6.  
  7. private static Span<char> Trim(Span<char> source)
  8. {
  9. if (source.IsEmpty)
  10. {
  11. return source;
  12. }
  13.  
  14. int start = 0, end = source.Length - 1;
  15. char startChar = source[start], endChar = source[end];
  16.  
  17. while ((start < end) && (startChar == ' ' || endChar == ' '))
  18. {
  19. if (startChar == ' ')
  20. {
  21. start++;
  22. }
  23.  
  24. if (endChar == ' ')
  25. {
  26. end—;
  27. }
  28.  
  29. startChar = source[start];
  30. endChar = source[end];
  31. }
  32.  
  33. return source.Slice(start, end - start + 1);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement