public int StrToInt(String str) {
if(str.length()<= 0){
return 0;
}
int firstIndex = 0;
boolean isNegative = false;
char first = str.charAt(0);
if(first == '+'|| first == '-'){
if(first == '-'){
isNegative = true;
}
firstIndex ++;
}
int index = firstIndex;
int value = 0;
while(index < str.length()){
int newValue = str.charAt(index) - '0';
if(newValue >= 0 && newValue<=9){
value = value*10+ newValue;
}else{
return 0;
}
index ++;
}
if(isNegative){
return -value;
}else{
return value;
}
}
String2Int
最新推荐文章于 2023-04-02 23:40:16 发布