如何在不使用Substring的情况下获取字符串的一部分

时间:2016-08-16 21:06:35

标签: c#

我想获取输出字符串的特定部分(服务器名称)。它正在工作,但是我需要返回更多字符,而我只想要服务器名称。

我的代码:

        Process process = new Process();
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = "/c nslookup -type=mx gmail.com"; // Note the /c command (*)
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.Start();
        //* Read the output (or the error)
        string output = process.StandardOutput.ReadToEnd();


        MessageBox.Show(output);

        string[] tokens = output.Split(' ');

        //take my server name
        string retVal = tokens[11];


        MessageBox.Show(retVal);

我的输出字符串:

Servidor:  UnKnown
Address:  192.168.0.1

Não é resposta autoritativa:
hotmail.com     MX preference = 5, mail exchanger = mx3.hotmail.com//I WANT THIS STRING  "mx3.hotmail.com", the first name server
hotmail.com     MX preference = 5, mail exchanger = mx4.hotmail.com
hotmail.com     MX preference = 5, mail exchanger = mx1.hotmail.com
hotmail.com     MX preference = 5, mail exchanger = mx2.hotmail.com

mx4.hotmail.com internet address = 207.46.8.167
mx4.hotmail.com internet address = 65.54.188.126
mx4.hotmail.com internet address = 65.54.188.110
mx4.hotmail.com internet address = 65.54.188.94
mx4.hotmail.com internet address = 65.55.37.104
mx4.hotmail.com internet address = 65.55.92.184
mx4.hotmail.com internet address = 65.55.92.168
mx4.hotmail.com internet address = 65.55.33.119
mx4.hotmail.com internet address = 207.46.8.199
...

我的retVal(变量名):

mx3.hotmail.com hotmail MX

我只想要“mx3.hotmail.com”

PS: retVal.Substring(0,14);不适合我,因为服务器名称lenths各不相同。

谢谢你! = d

3 个答案:

答案 0 :(得分:1)

使用String.Split(char[], StringSplitOptions)沿空格分割字符串:

var words = input.Split(new char[] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);

如果输入中有任何其他空白字符,如新行('\ n'),则可以将它们添加到分隔符数组中。如果此列表可能会更改,则可以进行配置。

然后,如果你知道你想要哪个单词,那是否永远不会改变:

if (words.Length > elementNo) result = words[elementNo];

如果单词可能位于列表中的任何位置,则需要扫描每个单词,直到找到所需的单词。

答案 1 :(得分:0)

不是将整个输出读入单个多行字符串,而是逐行读取并检查是否已达到所需的行。在公园里使用Substring()散步,因为你可以阅读到最后一行:

Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c nslookup -type=mx gmail.com"; // Note the /c command (*)
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();

string mx = "";
string delimiter = "mail exchanger = ";

string outputLine;
while ((outputLine = process.StandardOutput.ReadLine()) != null)
{
    if (outputLine.Contains(delimiter))
        mx = outputLine.Substring(outputLine.IndexOf(delimiter) + delimiter.Length);
}
if (string.IsNullOrEmpty(mx))
    Console.WriteLine("Lookup failed");
else
    Console.Write(mx);

答案 2 :(得分:0)

将正则表达式用于此类作业很常见,所以您也可以尝试以下方法:

// this will match anything after the last '=', before the newline
var regex = new Regex(@"MX preference = \d+, mail exchanger = (?<server>.*)\r\n");

// get the first match
var firstMatch = regex.Match(output);

// print the captured value
Console.WriteLine(firstMatch.Groups["server"].Value);

获得多场比赛也很简单:

// get all matches
var matches = regex.Matches(output);
foreach (Match m in matches)
    Console.WriteLine(m.Groups["server"].Value);