Q
C#只读整串
-7
A
回答
4
可以在/
分割字符串,然后使用int.TryParse
阵列的第一元件上,以查看它是否像的整数:
string str = "1234/xxxxx";
string[] array = str.Split(new []{'/'}, StringSplitOptions.RemoveEmptyEntries);
int number = 0;
if (str.Length == 2 && int.TryParse(array[0], out number))
{
//parsing successful.
}
else
{
//invalid number/string
}
Console.WriteLine(number);
1
使用IndexOf
和Substring
:
int indexOfSlash = text.IndexOf('/');
string beforeSlash = null;
int numBeforeSlash = int.MinValue;
if(indexOfSlash >= 0)
{
beforeSlash = text.Substring(0, indexOfSlash);
if(int.TryParse(beforeSlash, out numBeforeSlash))
{
// numBeforeSlash contains the real number
}
}
0
另一个替代方案:使用正则表达式:
var re = new System.Text.RegularExpression(@"^(\d+)/", RegexOptions.Compiled);
// ideally make re a static member so it only has to be compiled once
var m = re.Match(text);
if (m.IsMatch) {
var beforeSlash = Integer.Parse(re.Groups[0].Value);
}
相关问题
- 1. C++ ifstream的只读整数
- 2. C++字符串类是只读的吗?
- 3. C#只读串行端口数据来
- 4. 只读在C#
- 5. sqlite调整只读表
- 6. 整个模型为只读
- 7. C++打开只读
- 8. 当我读取整数后,C无法读取字符串
- 9. 阅读整个字符串
- 10. 如何从字符串,空格,新行和整数中的文件中只读取整数C
- 11. C++ cin正整数只有
- 12. MongoDb C#只读字段
- 13. Objective C只读int?什么?
- 14. 开放SQLITE只读在C#
- 15. 只读在C#VS在VB.NET
- 16. 只读角色c#mvc
- 17. 只读属性,在C#6.0
- 18. C#SqlDataReader只读取1行
- 19. asp.net,c#.net只读字段
- 20. Objective-C - 只读NSMutableArray和NSCoding?
- 21. C#的BindingList只读错误
- 22. C++错误:只读变量
- 23. C#make文件从只读中读取
- 24. 阅读整数用C
- 25. 阅读整数,在C++
- 26. 阅读整数矩阵(C++)
- 27. C++:getline()读完整行吗?
- 28. QtableView将整行设置为只读
- 29. Python整数到只读缓冲区
- 30. 如何使整个jqGrid禁用/只读
你有什么试过的?你看过字符串实例上可用的方法吗? – Magus
使用String.Split来获取零件和Convert.ToInt32以获取数字。或者使用SO搜索/ Google ... –