【问题标题】:How to convert string with some "\r\n" into list?如何将带有“\r\n”的字符串转换为列表?
【发布时间】:2015-03-06 05:09:33
【问题描述】:

我有一个字符串,想把它转换成LinkedList,这样每一行都是一个元素。

我使用 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

实际上,最好在左侧部分之后保存所有这些可用空间。

【问题讨论】:

  • Windows 环境中为\r\n,在Linux 中为\n
  • 你的意思是var lines = myString.Split(new [] { "\r\n", "\n" }, StringSplitOptions.None).ToList();
  • @InfernumDeus:你用什么类型的pars? KeyValuePair?
  • @CommuSoft,不确定是否得到您的要求。我只需要一对链接的 2 个字符串。
  • @InfernumDeus:是的,我得到了那个部分,但是你想如何存储这样的对?作为Tuple<string,string>KeyValuePair<string,string>,专用型,其他?

标签: c# regex string linked-list


【解决方案1】:

所以从您的问题中,我知道您需要将此字符串转换为链表 - 键、值对。这是一种方法:

编辑(抱歉这个问题也有可能编辑):

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,也就是键值对:

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

【讨论】:

  • 是的,我必须保存订单。
  • @InfernumDeus 在这里,如果我理解正确的话,我已将其更改为 LinkedList
  • @CommuSoft 是的,我一定错过了,已修复。
  • 看起来很棒!那么如何获取某个元素的值呢?
【解决方案2】:

换行符的表示方式取决于环境: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);
}

然后使用以下命令创建LinkedList

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

演示

使用 Mono 的 csharp 交互式外壳:

$ 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

【讨论】:

  • 非常适合懒惰评估
  • @paulroho:不仅仅是因为懒惰。 IEnumerable&lt;T&gt; 还强制人们更多地思考声明性,据说这种心态更安全...... ;)。
  • 如果您想放置易于运行的演示程序,请尝试dotnetfiddle.net,它是单声道 C# shell 的 Web 前端,可让您直接链接到您编写的程序。它非常适合在这个网站上回答。
  • @ScottChamberlain 感谢您的链接,太棒了!
【解决方案3】:

我了解您需要将字符串转换为列表。如果它是正确的,那么解决方案是

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

【讨论】:

    【解决方案4】:

    这是输出

    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
    

    【讨论】:

      【解决方案5】:

      是的,

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

      http://www.dotnetperls.com/newline

      【讨论】:

      • 这个问题的问题,是在真正的问题中只是在标题中指定...
      猜你喜欢
      • 2022-06-14
      • 2020-11-13
      • 2022-10-24
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 2020-06-05
      • 2020-07-30
      相关资源
      最近更新 更多