Character.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Text;
  2. namespace OrbitalDrydock.Functions
  3. {
  4. public class Character
  5. {
  6. public static string convertStringToAsciiPrintableSafe(string value)
  7. {
  8. if (value == null)
  9. {
  10. return string.Empty;
  11. }
  12. StringBuilder result = new StringBuilder();
  13. foreach (var current in value)
  14. {
  15. byte currentByte = (byte)current;
  16. if (currentByte < 32 || currentByte > 126)
  17. {
  18. //spaces char 32 are printable
  19. //ignore others
  20. }
  21. else
  22. {
  23. result.Append(current);
  24. }
  25. }
  26. return result.ToString();
  27. }
  28. public static string replaceWhitespace(string value, string replacement)
  29. {
  30. if (value == null)
  31. {
  32. return string.Empty;
  33. }
  34. StringBuilder result = new StringBuilder();
  35. foreach (var current in value)
  36. {
  37. byte currentByte = (byte)current;
  38. if (currentByte <= 32 || currentByte > 126)
  39. {
  40. result.Append(replacement);
  41. }
  42. else
  43. {
  44. result.Append(current);
  45. }
  46. }
  47. return result.ToString();
  48. }
  49. }
  50. }