代码之家  ›  专栏  ›  技术社区  ›  Алекса Јевтић

将行从特定字符子串到特定字符

c#
  •  1
  • Алекса Јевтић  · 技术社区  · 7 年前

    我有一个类似“4个程序文件(x86)2”的字符串。 4是行ID,2是父ID,中间的文本是内容。 如何从给定字符串中提取每个字符串,以便创建具有属性ID、内容和父ID的对象?

    6 回复  |  直到 7 年前
        1
  •  3
  •   Tim Schmelter    7 年前

    使用字符串方法,如 IndexOf/LastIndexOf Substring :

    int firstSpaceIndex = input.IndexOf(' ');
    int lastSpaceIndex = input.LastIndexOf(' ');
     // check that firstSpaceIndex != lastSpaceIndex and both are >= 0 because that would be invalid
    
    string id = input.Remove(firstSpaceIndex);
    string content = input.Substring(firstSpaceIndex + 1, lastSpaceIndex - firstSpaceIndex).Trim();
    string parentId = input.Substring(lastSpaceIndex).Trim();
    
        2
  •  1
  •   A Farmanbar    7 年前

    你应该使用

    一串ToCharArray方法()

    document

    例子:

    string myString =new string();
    
    char[] strChars=myString.ToCharArray();
    
    char lineID=strChars[0];
    
    char parentID=strChars[strChars.length-1];
    
    char[] content=new char[strChars.length-2];
    
    Buffer.BlockCopy(content,0,strChars,1,strchars.length-1);
    
    string strContent =new string(content);
    
        3
  •  1
  •   Idle_Mind    7 年前

    另一种方法使用 String.Split() List<String> String.Join() :

    string data = "4 Program Files (x86) 2";
    
    string LineID = "";
    string ParentID = "";
    string Content = "";
    List<string> values = new List<string>(data.Split(' '));
    if (values.Count >= 3)
    {
        LineID = values[0];
        ParentID = values[values.Count - 1];
        values.RemoveAt(0);
        values.RemoveAt(values.Count - 1);
        Content = String.Join(" ", values.ToArray());
    }
    
    Console.WriteLine("LineID = " + LineID);
    Console.WriteLine("ParentID = " + ParentID);
    Console.WriteLine("Content = " + Content);
    
        4
  •  0
  •   Ohlsen1980    7 年前

    可以使用正则表达式。在你的情况下,它会是这样的:

    string regex = "(?![0-9])(.*)(?=[0-9])";
    string toSplit = "4 Program Files (x86) 2";
    string [] result = Regex.Split(toSplit, regex);
    foreach (string s in result)
    {
        Console.Out.WriteLine(s);
    }
    Console.ReadLine();
    

    数组中的第二个字符串是结果。参考系。文本正则表达式;在你的课堂上。

        5
  •  0
  •   Syl20    7 年前

    此解决方案使用Substring(),但它假设您事先知道parent\u ID和line\u ID的长度(以下代码中的1)。。。

        string mystr = "4 Program Files (x86) 2";
        mystr = mystr.Substring(1, mystr.Length - 2);
        Console.WriteLine(mystr);// Program Files (x86) 
    
        mystr = "4 Program Files (x86) 2";
        mystr = mystr.Substring(0, 1);
        Console.WriteLine(mystr);//print 4
    
        mystr = "4 Program Files (x86) 2";
        mystr = mystr.Substring( mystr.Length-1, 1);
        Console.WriteLine(mystr);//print 2
    
        6
  •  0
  •   Nielson R. O.    7 年前

    这应该行得通

            var str = "4 Program Files (x86) 2";
            var splitted = str.Split(' '); // splits the string
            var id = splitted[0]; // the first position of the array is the id
            var parentId = splitted[splitted.Length - 1]; // the last position of the array is the parentId
    
            var content = str.Substring(id.Length, str.Length - id.Length - parentId.Length); // gets the content between the id and parentId