Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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# 如何从字符串c中提取数字_C# - Fatal编程技术网

C# 如何从字符串c中提取数字

C# 如何从字符串c中提取数字,c#,C#,我有一根绳子 "transform(23, 45)" 从这个字符串中,我必须提取23和45,是的 var xy = "transform(23,45)".Substring("transform(23,45)".indexOf('(') + 1).TrimEnd(')'); var num = xy.Split(','); 我用的是c。在c语言中有更好的方法吗?使用正则表达式: string sInput = "transform(23, 45)"; Match match = Regex.

我有一根绳子

"transform(23, 45)"
从这个字符串中,我必须提取23和45,是的

var xy = "transform(23,45)".Substring("transform(23,45)".indexOf('(') + 1).TrimEnd(')');
var num = xy.Split(',');

我用的是c。在c语言中有更好的方法吗?

使用正则表达式:

string sInput = "transform(23, 45)";
Match match = Regex.Match(sInput, @"(\d)+",
              RegexOptions.IgnoreCase);

if (match.Success)
{
    foreach (var sVal in match)
             // Do something with sVal
}
您可以阅读更多关于正则表达式的内容。
用于训练,帮助很大

简单的正则表达式字符串应该是[0-9]+,但您可能需要定义其他表达式约束,例如,您如何处理字符串中的句点、逗号等

var matches = Regex.Matches("transform(23,45)", "([0-9]+)");
foreach (Match match in matches)
{  
    int value = int.Parse(match.Groups[1].Value);
    // Do work.
}
这就行了

string[] t = "transform(23, 45)".ToLower().Replace("transform(", string.Empty).Replace(")", string.Empty).Split(',');
使用正则表达式:

说明:

\d    Matches any decimal digit.

\d+   Matches digits (0-9) 
      (1 or more times, matching the most amount possible) 
及使用:

foreach (Match match in matches)
{  
    var number = match.Groups[1].Value;
}

+1表示句点、逗号等,并添加到列表中:非10基数字文本、强制类型文本,但其他编程语言也有它们,等等。
foreach (Match match in matches)
{  
    var number = match.Groups[1].Value;
}