123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 loyalsoft. All rights reserved.
- // Homepage: http://www.game7000.com/
- // Feedback: http://www.game7000.com/
- //------------------------------------------------------------
- /// <summary>
- /// 对 string 的扩展方法。
- /// </summary>
- public static class StringExtension
- {
- /// <summary>
- /// 从指定字符串中的指定位置处开始读取一行。
- /// </summary>
- /// <param name="rawString">指定的字符串。</param>
- /// <param name="position">从指定位置处开始读取一行,读取后将返回下一行开始的位置。</param>
- /// <returns>读取的一行字符串。</returns>
- public static string ReadLine(this string rawString, ref int position)
- {
- if (position < 0)
- {
- return null;
- }
- int length = rawString.Length;
- int offset = position;
- while (offset < length)
- {
- char ch = rawString[offset];
- switch (ch)
- {
- case '\r':
- case '\n':
- if (offset > position)
- {
- string line = rawString.Substring(position, offset - position);
- position = offset + 1;
- if ((ch == '\r') && (position < length) && (rawString[position] == '\n'))
- {
- position++;
- }
- return line;
- }
- offset++;
- position++;
- break;
- default:
- offset++;
- break;
- }
- }
- if (offset > position)
- {
- string line = rawString.Substring(position, offset - position);
- position = offset;
- return line;
- }
- return null;
- }
- }
|