StringExtension.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. /// <summary>
  8. /// 对 string 的扩展方法。
  9. /// </summary>
  10. public static class StringExtension
  11. {
  12. /// <summary>
  13. /// 从指定字符串中的指定位置处开始读取一行。
  14. /// </summary>
  15. /// <param name="rawString">指定的字符串。</param>
  16. /// <param name="position">从指定位置处开始读取一行,读取后将返回下一行开始的位置。</param>
  17. /// <returns>读取的一行字符串。</returns>
  18. public static string ReadLine(this string rawString, ref int position)
  19. {
  20. if (position < 0)
  21. {
  22. return null;
  23. }
  24. int length = rawString.Length;
  25. int offset = position;
  26. while (offset < length)
  27. {
  28. char ch = rawString[offset];
  29. switch (ch)
  30. {
  31. case '\r':
  32. case '\n':
  33. if (offset > position)
  34. {
  35. string line = rawString.Substring(position, offset - position);
  36. position = offset + 1;
  37. if ((ch == '\r') && (position < length) && (rawString[position] == '\n'))
  38. {
  39. position++;
  40. }
  41. return line;
  42. }
  43. offset++;
  44. position++;
  45. break;
  46. default:
  47. offset++;
  48. break;
  49. }
  50. }
  51. if (offset > position)
  52. {
  53. string line = rawString.Substring(position, offset - position);
  54. position = offset;
  55. return line;
  56. }
  57. return null;
  58. }
  59. }