如果 String.Split(String[]) 不存在,你会如何分割 \r\n ?

发布于 2024-08-17 08:30:04 字数 377 浏览 4 评论 0原文

使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

那片花海 2024-08-24 08:30:04

您可以

string[] lines = doc.Split('\n');
for (int i = 0; i < lines.Length; i+= 1)
   lines[i] = lines[i].Trim();

假设 µF 完全支持 Trim()。 Trim() 将删除所有空格,这可能很有用。否则使用 TrimEnd('\r')

You can do

string[] lines = doc.Split('\n');
for (int i = 0; i < lines.Length; i+= 1)
   lines[i] = lines[i].Trim();

Assuming that the µF supports Trim() at all. Trim() will remove all whitespace, that might be useful. Otherwise use TrimEnd('\r')

墨小墨 2024-08-24 08:30:04

我将循环遍历文档中的每个字符,因为这显然是必需的。您认为 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).

痴情换悲伤 2024-08-24 08:30:04

如何以有效的方式将文档分割成行(例如,不要在文档中的每个字符上疯狂循环)?

您认为内置的 Split 是如何工作的?

只需自己重新实现它作为扩展方法即可。

How can I split a document into lines in an efficient manner (e.g. not looping madly across each char in the doc)?

How do you think the built-in Split works?

Just reimplement it yourself as an extension method.

余罪 2024-08-24 08:30:04

怎么样:

string path = "yourfile.txt";
string[] lines = File.ReadAllLines(path);

或者

string content = File.ReadAllText(path);
string[] lines = content.Split(
    Environment.NewLine.ToCharArray(),
    StringSplitOptions.RemoveEmptyEntries);

阅读 .NET Micro Framework 3.0,这段代码可以工作:

string line = String.Empty;
StreamReader reader = new StreamReader(path);
while ((line = reader.ReadLine()) != null)
{
    // do stuff
}

What about:

string path = "yourfile.txt";
string[] lines = File.ReadAllLines(path);

Or

string content = File.ReadAllText(path);
string[] lines = content.Split(
    Environment.NewLine.ToCharArray(),
    StringSplitOptions.RemoveEmptyEntries);

Readind that .NET Micro Framework 3.0, this code can work:

string line = String.Empty;
StreamReader reader = new StreamReader(path);
while ((line = reader.ReadLine()) != null)
{
    // do stuff
}
陌伤浅笑 2024-08-24 08:30:04

这在某些情况下可能会有所帮助:

StreamReader reader = new StreamReader(file);    
string _Line = reader.ReadToEnd();
string IntMediateLine = string.Empty;
IntMediateLine = _Line.Replace("entersign", "");
string[] ArrayLineSpliter = IntMediateLine.Split('any specail chaarater');

This may help in some scenario:

StreamReader reader = new StreamReader(file);    
string _Line = reader.ReadToEnd();
string IntMediateLine = string.Empty;
IntMediateLine = _Line.Replace("entersign", "");
string[] ArrayLineSpliter = IntMediateLine.Split('any specail chaarater');
通知家属抬走 2024-08-24 08:30:04

如果您想要一个适用于整个字符串MicroFramework兼容拆分函数,这里有一个可以解决这个问题的函数,类似于使用 StringSplitOptions 的常规框架版本。没有任何:

    private static string[] Split(string s, string delim)
    {
        if (s == null) throw new NullReferenceException();

        // Declarations
        var strings = new ArrayList();
        var start = 0;

        // Tokenize
        if (delim != null && delim != "")
        {
            int i;
            while ((i = s.IndexOf(delim, start)) != -1)
            {
                strings.Add(s.Substring(start, i - start));
                start = i + delim.Length;
            }
        }

        // Append left over
        strings.Add(s.Substring(start));

        return (string[]) strings.ToArray(typeof(string));
    }

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:

    private static string[] Split(string s, string delim)
    {
        if (s == null) throw new NullReferenceException();

        // Declarations
        var strings = new ArrayList();
        var start = 0;

        // Tokenize
        if (delim != null && delim != "")
        {
            int i;
            while ((i = s.IndexOf(delim, start)) != -1)
            {
                strings.Add(s.Substring(start, i - start));
                start = i + delim.Length;
            }
        }

        // Append left over
        strings.Add(s.Substring(start));

        return (string[]) strings.ToArray(typeof(string));
    }
无风消散 2024-08-24 08:30:04

您可以用子字符串拆分字符串。

String.Split(new string[] { "\r\n" }, StringSplitOptions.None);

You can split your string with a substring.

String.Split(new string[] { "\r\n" }, StringSplitOptions.None);
情徒 2024-08-24 08:30:04
String theText = "word1 \n word2 \n word3";

string[] lines = theText.Split( new string[] { "\r\n", "\r", "\n"}, StringSplitOptions.None);

string[] lines = theText.Split( new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries );

https://spacetech.dk/how-to-split -on-newline-in-c.html

https://www. bytehide.com/blog/split-string-csharp

String theText = "word1 \n word2 \n word3";

string[] lines = theText.Split( new string[] { "\r\n", "\r", "\n"}, StringSplitOptions.None);

or

string[] lines = theText.Split( new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries );

https://spacetech.dk/how-to-split-on-newline-in-c.html

https://www.bytehide.com/blog/split-string-csharp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文