Last active
January 1, 2016 02:19
-
-
Save MangoLiu/8078801 to your computer and use it in GitHub Desktop.
String--->int (将字符串转换为整型)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int str2Int(String s) { | |
// method1 | |
int n = Integer.parseInt(s); | |
// method2 | |
int sum = 0; | |
int t = 1; | |
char[] c = s.toCharArray(); | |
for (int i = c.length - 1; i > -1; i--) { | |
sum += (c[i] - '0') * t; | |
t *= 10; | |
} | |
return sum;/* (or n) */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment