如何检查是字符串还是数字 [英] How to check is a string or number
本文介绍了如何检查是字符串还是数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组 ["abc", "94761178","790"]
我想迭代每个并检查是字符串还是整数?
I want to iterate each and check is a String or an Integer?
如何检查?
如何将123"转换为整数123
How to convert "123" to integer 123
推荐答案
编辑 Swift 2.2:
在 swift 2.2 中使用 Int(yourArray[1])
In swift 2.2 use Int(yourArray[1])
var yourArray = ["abc", "94761178","790"]
var num = Int(yourArray[1])
if num != nil {
println("Valid Integer")
}
else {
println("Not Valid Integer")
}
它会告诉你字符串是有效的整数,num
包含有效的 Int
.你可以用 num
进行计算.
It will show you that string is valid integer and num
contains valid Int
.You can do your calculation with num
.
来自文档:
如果字符串表示一个适合 Int 的整数,则返回相应的整数.这接受与常规匹配的字符串仅表达[-+]?[0-9]+".
If the string represents an integer that fits into an Int, returns the corresponding integer.This accepts strings that match the regular expression "[-+]?[0-9]+" only.
这篇关于如何检查是字符串还是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文