based on 'Cristina De Rito' solution here is a little playgrud routine for checking results, you can add your own CharacterSet
(swift 5)
var arr_IsStringNumberOnly:[String] = ["123", "456.7", "890,1", "1'234", "", "abc", "def3", "xyz 4", "5 äöü"]
for i in 0..<arr_IsStringNumberOnly.count {
//the code w/o 'nil'
CharacterSet.init(arrayLiteral: "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ",", ".", "'" ).isSuperset(of: CharacterSet(charactersIn: arr_IsStringNumberOnly[i]))
//the code with handling 'nil' (nil dosent work in test array)
//CharacterSet.init(arrayLiteral: "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ",", ".", "'" ).isSuperset(of: CharacterSet(charactersIn: arr_IsStringNumberOnly ?? "abc")) //if empty assume it's text
//the code with handling 'nil' (nil dosent work in test array)
//CharacterSet.init(arrayLiteral: "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ",", ".", "'" ).isSuperset(of: CharacterSet(charactersIn: arr_IsStringNumberOnly ?? "123")) //if empty assume it's numeric
//presentation:
print("arr_IsStringNumberOnly[i]: \(arr_IsStringNumberOnly[i]) \t '\(CharacterSet.init(arrayLiteral: "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ",", ".", "'" ).isSuperset(of: CharacterSet(charactersIn: arr_IsStringNumberOnly[i])) )'")
}
Presents:
- arr_IsStringNumberOnly[i]: 123 'true'
- arr_IsStringNumberOnly[i]: 456.7 'true'
- arr_IsStringNumberOnly[i]: 890,1 'true'
- arr_IsStringNumberOnly[i]: 1'234 'true'
- arr_IsStringNumberOnly[i]: 'true'
- arr_IsStringNumberOnly[i]: abc 'false'
- arr_IsStringNumberOnly[i]: def3 'false'
- arr_IsStringNumberOnly[i]: xyz 4 'false'
- arr_IsStringNumberOnly[i]: 5 äöü 'false'