# An example of a string to number method implemented by Swift algorithm

• 2020-05-24 06:17:15
• OfStack

preface

Recently learned Swift after 1 straight no chance of actual combat, found that due to history Swift, most algorithms are used on the Internet C, Java or any other language, almost no use Swift implement, so he plans to use Swift algorithm to achieve more mainstream, both Swift reviews on their own, is algorithm improvement for yourself.

The first is to use Swift to convert strings to Numbers. Of course, api cannot be used to convert strings to Numbers with Swift.

Topic:

Use Swift to implement 1 method, input string, output the string into the number.

For example, enter the string "125" and output the number 125

Implementation ideas and code

First, consider possible input, including illegal input:

Case 1: all characters can be directly converted to Numbers, such as "125"

Case 2: contains one or more plus or minus signs, such as "-125", "hang +125"

Case 3: contains illegal characters, such as "125lff"

In case 1, the process is simple: first, each character of the string "125" is converted to a number using the ASCII encoding, then it is converted to a number using multiplication and addition: 1*100+2*10+3=123.

However, the actual situation is not so simple, because of the existence of case 2 and case 3, it is definitely not feasible to directly use multiplication and addition above, and the corresponding judgment must be added in the middle. The best way is to walk through the characters in the string directly, assuming there are no signs or illegal characters. intStr=1 when walking through the first character "1", intStr = intStr*10+2=12 when walking through the second character "2",intStr = intStr*10+3=123 when walking through the last character "3".

As for "+" and "-", they are positive or negative only if they are at the beginning of the string, and they are illegal if they exist in the middle of the string. We can judge "+" and "-" (corresponding values are 43 and 45, respectively) by ASCII encoding, and set a variable s to represent the accumulated plus or minus of multiple "+" and "-". The final result is intStr = s * intStr. When an illegal character is found while traversing the string, the subsequent traversal is terminated and prompted.

Implementation code:

``````
// String to digit
func StrToInt(str:String) -> Int{

// The string cannot be empty
guard str.isEmpty == false else {
print(" The string cannot be empty ~");
return 0;
}

var s = 1
var strInt:Int? = nil

for characterInt in str.unicodeScalars {

// Can contain only Numbers or signs
let tempStrInt = characterInt.hashValue - "0".unicodeScalars.first!.hashValue
guard (tempStrInt <= 9 && tempStrInt >= 0) || (characterInt.hashValue == 43 || characterInt.hashValue == 45) else {

print(" Contains illegal characters! ");
return 0;

}

// A plus or minus sign can only exist at the beginning of a string
if characterInt.hashValue == 43 || characterInt.hashValue == 45 {
guard strInt == nil else {
print(" Signs can only exist at the beginning of a string! ");
return 0;
}
}

// Now that we're here 1 Step that says the string is valid
// Judge positive and negative
if characterInt.hashValue == 43 || characterInt.hashValue == 45{
s = s * ( 44 - characterInt.hashValue )
}else{

if strInt == nil {
strInt = characterInt.hashValue - "0".unicodeScalars.first!.hashValue
}else{
// Use the overflow operator &* and &+ Avoid overflow crashes when values are too large
strInt = strInt! &* 10 &+ ( characterInt.hashValue - "0".unicodeScalars.first!.hashValue )
}

}

}

var result:Int? = 0
if strInt != nil {
result = s * strInt!
}

return result!;
}``````

In the algorithm implemented above:

Input "125" and output "125"

Input "+-125" and output "-125"

Type "1-25" to indicate that "plus or minus" can only exist at the beginning of a string!"

Enter "1m25", and the prompt "contains illegal characters"

conclusion

Related articles: