简体   繁体   English

最好的方法是在C#中的字符串中获取第一个单词和其余单词

[英]Best way get first word and rest of the words in a string in C#

In C# 在C#中

var parameters =
    from line in parameterTextBox.Lines
    select new {name = line.Split(' ').First(), value = line.Split(' ').Skip(1)};

Is there a way to do this without having to split twice? 有没有办法做到这一点,而不必分裂两次?

you can store the split in a let clause 您可以将拆分存储在let子句中

var parameters =
    from line in parameterTextBox.Lines
    let split = line.Split(' ')
    select new {name = split.First(), value = split.Skip(1)};

Sure. 当然。

var parameters = from line in parameterTextBox.Lines
                 let words = line.Split(' ')
                 select new { name = words.First(), words.skip(1) };
string Str= "one all of the rest";
Match m = Regex.match(Str,"(\w*) (\w.*)");
string wordone = m.Groups[1];
string wordtwo = m.Groups[2];

You might try this: 你可以试试这个:

private Dictionary<string, string> getParameters(string[] lines)
{
    Dictionary<string, string> results = new Dictionary<string, string>();
    foreach (string line in lines)
    {
        string pName = line.Substring(0, line.IndexOf(' '));
        string pVal = line.Substring(line.IndexOf(' ') + 1);
        results.Add(pName, pVal);
    }
    return results;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM