C# 如果 String.Split(String[]) 不存在,您将如何通过 \r\n 拆分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2038920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would you split by \r\n if String.Split(String[]) did not exist?
提问by AngryHacker
Using the .NET MicroFramework which is a really cut-down version of C#. For instance, System.Stringbarely has any of the goodies that we've enjoyed over the years.
使用 .NET MicroFramework,它是 C# 的真正精简版。例如,System.String几乎没有我们多年来喜欢的任何优点。
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.
我需要将文本文档拆分成行,这意味着按\r\n 拆分。但是,String.Split 仅提供按字符而不是按字符串的拆分。
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.
PS System.String 也缺少 Replace 方法,因此不起作用。
PPS Regex 也不是 MicroFramework 的一部分。
采纳答案by Henk Holterman
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')
假设 μF 完全支持 Trim()。Trim() 将删除所有空格,这可能很有用。否则使用TrimEnd('\r')
回答by Rubens Farias
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:
阅读.NET Micro Framework 3.0,此代码可以工作:
string line = String.Empty;
StreamReader reader = new StreamReader(path);
while ((line = reader.ReadLine()) != null)
{
// do stuff
}
回答by Jon Skeet
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.
我会遍历文档中的每个字符,因为这显然是必需的。你觉得String.Split
效果如何?然而,我会尝试只击中每个角色一次。
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).
保留迄今为止找到的字符串列表。IndexOf
重复使用,将当前偏移量传入字符串(即前一个匹配项 + 2)。
回答by Anon.
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?
您认为内置的Split
工作原理如何?
Just reimplement it yourself as an extension method.
只需自己重新实现它作为扩展方法。
回答by AbdulAzizFarooqi
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');
回答by vGHazard
If you'd like a MicroFramework compatiblesplit 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:
如果您想要一个适用于整个字符串的MicroFramework 兼容拆分函数,这里有一个可以解决问题的函数,类似于使用 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));
}