我正在尝试在 C# 中执行 Parse int,但在 int mFrom = int.Parse(fromTo.Substring(1, fromTo.IndexOf("C")));
行中出现异常像下面一样
"System.FormatException: Input string was not in a correct format.
代码如下:
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string fromTo= "M5000C001";
int mFrom = int.Parse(fromTo.Substring(1, fromTo.IndexOf("C")));
int cFrom = int.Parse(fromTo.Substring(fromTo.LastIndexOf("C") + 1));
Console.WriteLine("FromTo" + mFrom);
}
}
最佳答案
试试这个:
IndexOf
将返回您所请求的字 rune 字的索引。
string fromTo= "M5000C001";
int mFrom = int.Parse(fromTo.Substring(1, fromTo.IndexOf("C") - 1));
int cFrom = int.Parse(fromTo.Substring(fromTo.LastIndexOf("C") + 1));
Console.WriteLine("FromTo" + mFrom);
引用这个documentation .
关于c# - 在 C# 中执行 ParseInt 时输入字符串的格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45348143/