2015-01-08 46 views
2

我有一个字符串,并希望将其转换为LinkedList,以便每行都是单个元素。如何将字符串与一些“ r n”转换为列表?

我使用Visual Studio 2012

有没有简单的方法来做到这一点?

这个字符串的样子:

LOAD =5.01E+10 
MPY $1 
ADD $2 

但实际上它包含LOAD =5.01E+10\r\nMPY $1\r\nADD $2

重要更新:我并不需要删除 “\ r \ n” 或这样的事情。我需要在每个元素中使用“左边部分”和“右边部分”对的列表(实际上不链接)。什么样的名单我怎样从前面的例子中得到举例:

("LOAD ", "=5.01E+10") => ("MPY ", "$1") => ("ADD ", "$2") => null 

其实,更好地节省左侧毕竟这个自由的空间。

+0

在* Windows *环境中,它是'\ r \ n',在* Linux *上,它是'\ n'。 –

+0

你的意思是'var lines = myString.Split(new [] {“\ r \ n”,“\ n”},StringSplitOptions.None).ToList();' – StuartLC

+0

@InfernumDeus:你用什么类型帕尔斯? 'KeyValuePair'? –

回答

4

所以,从你的问题,我得到你需要这个字符串变成一个链表 - 键,值对。这里是一个办法做到这一点:

编辑(抱歉,这个问题有太多可编辑):

string input = "LOAD =5.01E+10\r\nMPY $1\r\nADD $2"; 

IEnumerable<KeyValuePair<string, string>> pairs = 
    input.Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) 
     .Select(x => x.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)) 
     .Select(x => new KeyValuePair<string, string>(x[0], x[1])); 

var data = new LinkedList<KeyValuePair<string, string>>(pairs); 

编辑

这里是你如何遍历链表:

foreach (KeyValuePair<string, string> pair in data) { 
    Console.WriteLine("Key: {0} - Value: {1}", pair.Key, pair.Value); 
} 

或者得到一个节点并访问它的Value,这是key-val ue pair:

KeyValuePair<string, string> p = data.First.Value; 
string k = p.Key; // LOAD 
string v = p.Value; // =5.01E+10 
+0

这是一个'Dictionary',订单不再维护... –

+1

是的,我必须保存订单。 – InfernumDeus

+2

@InfernumDeus在这里,如果我正确地理解了你的话,我已经将它改为了'LinkedList'。 –

0

是,

yourString = yourString.Replace(System.Environment.NewLine, "\r\n"); 

http://www.dotnetperls.com/newline

+1

这个问题的问题是,在真正的问题是只在标题中指定... –

0

这里是输出

string LOAD = "5.01E+10\r\nMPY $1\r\nADD $2"; 
string newstring1 = LOAD.Replace("\r", " "); 
string newstring2 = newstring1.Replace("\n", " "); 
Console.WriteLine(newstring2); 

输出

5.01E+10 MPY $1 ADD $2 
4

换行表示可以依赖于环境的方法:Linux例如使用\n

为了方便起见,你可以使用一个StringReader

public static IEnumerable<string> Lines (string text) { 
    string line; 
    using (StringReader reader = new StringReader(text)) { 
     while ((line = reader.ReadLine()) != null) { 
      yield return line; 
     } 
    } 
} 

懒惰评估行:这可能在未来有所帮助,如果你比如有一个巨大的文件,只需要表现出前10行给用户。

然后调用:

return new LinkedList<string>(Lines(text)); 

编辑:

要获得对,你可以引入一个新的方法:

第二种方法则可以使用拆分项目成对:

public static KeyValuePair<string,string> SplitToKeyValue(string text) { 
    Regex p = new Regex(@"^(\w+)\s+(.*)$"); 
    Match m = p.Match(text); 
    return new KeyValuePair<string,string>(m.Groups[1].Value,m.Groups[2].Value); 
} 

然后cre吃LinkedList有:使用Mono的csharp交互shell

return new LinkedList<KeyValuePair<string,string>>(Lines(text).Select(SplitToKeyValue)); 

DEMO

$ csharp 
Mono C# Shell, type "help;" for help 

Enter statements below. 
csharp> using System.IO; 
csharp> using System.Text.RegularExpressions; 
csharp> public static class Foo { 
     > 
     > public static KeyValuePair<string,string> SplitToKeyValue(string text) { 
     >  Regex p = new Regex(@"^(\w+)\s+(.*)$"); 
     >  Match m = p.Match(text); 
     >  return new KeyValuePair<string,string>(m.Groups[1].Value,m.Groups[2].Value); 
     > } 
     > 
     > public static IEnumerable<string> Lines (string text) { 
     >  string line; 
     >  using (StringReader reader = new StringReader(text)) { 
     >   while ((line = reader.ReadLine()) != null) { 
     >    yield return line; 
     >   } 
     >  } 
     > } 
     > 
     > } 
csharp> string inp = "LOAD =5.01E+10\nMPY $1\nADD $2"; 
csharp> new LinkedList<KeyValuePair<string,string>>(Foo.Lines(inp).Select(Foo.SplitToKeyValue)) 
{ [LOAD, =5.01E+10], [MPY, $1], [ADD, $2] } 

fiddle

+1

伟大的去懒惰评估的路线 – paulroho

+0

@ paulroho:不仅因为懒惰。 'IEnumerable '也强制人们思考更多*声明性*,一种据说更安全的心态......)。 –

+2

如果你想放置易于运行的演示去尝试https://dotnetfiddle.net/它是一个单一的C#shell的Web前端,并让你有直接运行的链接到你写的程序。它非常适合在这个网站上找到答案。 –

0

我明白你需要将字符串转换为列表。如果是正确的,然后解决方案是

Regex.Matches("your string","\r\n", RegexOptions.Singleline|RegexOptions.IgnoreCase)