Utility.cs 902 B

1234567891011121314151617181920212223242526272829303132333435
  1. namespace VSCodeEditor
  2. {
  3. public static class Utility
  4. {
  5. public static string FileNameWithoutExtension(string path)
  6. {
  7. if (string.IsNullOrEmpty(path))
  8. {
  9. return "";
  10. }
  11. var indexOfDot = -1;
  12. var indexOfSlash = 0;
  13. for (var i = path.Length - 1; i >= 0; i--)
  14. {
  15. if (indexOfDot == -1 && path[i] == '.')
  16. {
  17. indexOfDot = i;
  18. }
  19. if (indexOfSlash == 0 && path[i] == '/' || path[i] == '\\')
  20. {
  21. indexOfSlash = i + 1;
  22. break;
  23. }
  24. }
  25. if (indexOfDot == -1)
  26. {
  27. indexOfDot = path.Length - 1;
  28. }
  29. return path.Substring(indexOfSlash, indexOfDot - indexOfSlash);
  30. }
  31. }
  32. }