Question

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.

Was it helpful?

Solution

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')

OTHER TIPS

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).

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.

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
}

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');

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));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top