Skip to content

Instantly share code, notes, and snippets.

@MangoLiu
Last active January 1, 2016 02:19
Show Gist options
  • Save MangoLiu/8078801 to your computer and use it in GitHub Desktop.
Save MangoLiu/8078801 to your computer and use it in GitHub Desktop.
String--->int (将字符串转换为整型)
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