Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从后跟“QUOTE”的字符串中提取数字;我—“我认为;_C#_String_Extract - Fatal编程技术网

C# 从后跟“QUOTE”的字符串中提取数字;我—“我认为;

C# 从后跟“QUOTE”的字符串中提取数字;我—“我认为;,c#,string,extract,C#,String,Extract,在C#中,我将日志文件中的最后一行指定给字符串“str” 我想提取“I-”后面的数字,并将其存储在数据类型中,以便以后在程序中使用。在下面的示例中,我需要获得667466。 请帮忙怎么做 string str = "2017-01-16 11:10:37.151 +11:00 [Information] OutboundMessageFile:MessageFields: [KeyValuePair`2 { Key: \"ConfirmationCode\", Value: \"I-667466

在C#中,我将日志文件中的最后一行指定给字符串“str” 我想提取“I-”后面的数字,并将其存储在数据类型中,以便以后在程序中使用。在下面的示例中,我需要获得667466。
请帮忙怎么做

string str = "2017-01-16 11:10:37.151 +11:00 [Information] OutboundMessageFile:MessageFields: [KeyValuePair`2 { Key: \"ConfirmationCode\", Value: \"I-667466\" }], MessageAttachments: \"\", MessageConfig: \"\", MachineName: \"MARYDEV234\" }"

您可以使用正则表达式查找此数据

var regex = new Regex("Value: \"I-(\\d+)\"");
var match = regex.Match(str);
if (match.Success)
{
    var data = match.Groups[1].Value;
    // Store data for next use
}

我个人喜欢Uladzimir的正则表达式解决方案,但这里有一个替代方案:

string str = "2017-01-16 11:10:37.151 +11:00 [Information] OutboundMessageFile:MessageFields: [KeyValuePair`2 { Key: \"ConfirmationCode\", Value: \"I-667466\" }], MessageAttachments: \"\", MessageConfig: \"\", MachineName: \"MARYDEV234\" }";
int index = str.IndexOf("I-");
index += 2; //Skip the "I-"
string output = "";
for(int i = index; true; i++)
{
    if(str[i] == '"')
        break;
    output += str[i];
}
Console.WriteLine(output);

这只适用于特定格式的字符串,就像您所拥有的一样,正则表达式解决方案更具通用性。您可以升级此版本,使
output
成为StringBuilder,而不是字符串。

谢谢Uladzimir,但当我使用上述代码时,“{Value:“i-667466”}”存储在“match”中,然后var数据为null。。。。667466未存储在var数据中。