如果 String.Split(String[]) 不存在,你会如何分割 \r\n ?
使用 .NET MicroFramework,它是 C# 的真正精简版本。例如,System.String 几乎没有我们所提供的任何好处多年来我一直很享受。
我需要将文本文档拆分为行,这意味着按 \r\n 拆分。但是,String.Split 只提供按字符分割,而不提供按字符串分割。
如何以有效的方式将文档分割成行(例如,不要疯狂地循环遍历文档中的每个字符)?
PS System.String 还缺少 Replace 方法,因此无法工作。
PPS Regex 也不是 MicroFramework 的一部分。
Using the .NET MicroFramework which is a really cut-down version of C#. For instance, System.String barely has any of the goodies that we've enjoyed over the years.
I need to split a text document into lines, which means splitting by \r\n. However, String.Split only provides a split by char, not by string.
How can I split a document into lines in an efficient manner (e.g. not looping madly across each char in the doc)?
P.S. System.String is also missing a Replace method, so that won't work.
P.P.S. Regex is not part of the MicroFramework either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以
假设 µF 完全支持 Trim()。 Trim() 将删除所有空格,这可能很有用。否则使用
TrimEnd('\r')
You can do
Assuming that the µF supports Trim() at all. Trim() will remove all whitespace, that might be useful. Otherwise use
TrimEnd('\r')
我将循环遍历文档中的每个字符,因为这显然是必需的。您认为
String.Split
是如何工作的?不过,我会尝试只击中每个角色一次。保留迄今为止找到的字符串列表。重复使用
IndexOf
,将当前偏移量传递到字符串中(即上一个匹配项 + 2)。I would loop across each char in the document, because that's clearly required. How do you think
String.Split
works? I would try to do so only hitting each character once, however.Keep a list of strings found so far. Use
IndexOf
repeatedly, passing in the current offset into the string (i.e. the previous match + 2).您认为内置的
Split
是如何工作的?只需自己重新实现它作为扩展方法即可。
How do you think the built-in
Split
works?Just reimplement it yourself as an extension method.
怎么样:
或者
阅读 .NET Micro Framework 3.0,这段代码可以工作:
What about:
Or
Readind that .NET Micro Framework 3.0, this code can work:
这在某些情况下可能会有所帮助:
This may help in some scenario:
如果您想要一个适用于整个字符串的MicroFramework兼容拆分函数,这里有一个可以解决这个问题的函数,类似于使用 StringSplitOptions 的常规框架版本。没有任何:
If you'd like a MicroFramework compatible split function that works for an entire string of characters, here's one that does the trick, similar to the regular frameworks' version using StringSplitOptions.None:
您可以用子字符串拆分字符串。
You can split your string with a substring.
或
https://spacetech.dk/how-to-split -on-newline-in-c.html
https://www. bytehide.com/blog/split-string-csharp
or
https://spacetech.dk/how-to-split-on-newline-in-c.html
https://www.bytehide.com/blog/split-string-csharp