public int myAtoi(String str) {
String strResult="";
if(str == null || str.trim().equals("")){
return 0;
}
char[] no_space_char = str.trim().toCharArray();
boolean isPlus = no_space_char[0] == '-'?false:true;
boolean hasPlus = no_space_char[0] == '+'?true:false;
if((!isPlus || hasPlus) && no_space_char.length==1)
return 0;
for(int i = !isPlus || hasPlus ? 1:0; i < no_space_char.length; i++){
if(!(no_space_char[i] >= '0' && no_space_char[i] <= '9')){
break;
}else{
strResult+=no_space_char[i];
}
}
int result;
try {
if("".equals(strResult)){
return 0;
}
result = Integer.valueOf(strResult);
} catch (NumberFormatException e) {
return isPlus?Integer.MAX_VALUE:Integer.MIN_VALUE;
}
return isPlus?result:-result;
}
String strResult="";
if(str == null || str.trim().equals("")){
return 0;
}
char[] no_space_char = str.trim().toCharArray();
boolean isPlus = no_space_char[0] == '-'?false:true;
boolean hasPlus = no_space_char[0] == '+'?true:false;
if((!isPlus || hasPlus) && no_space_char.length==1)
return 0;
for(int i = !isPlus || hasPlus ? 1:0; i < no_space_char.length; i++){
if(!(no_space_char[i] >= '0' && no_space_char[i] <= '9')){
break;
}else{
strResult+=no_space_char[i];
}
}
int result;
try {
if("".equals(strResult)){
return 0;
}
result = Integer.valueOf(strResult);
} catch (NumberFormatException e) {
return isPlus?Integer.MAX_VALUE:Integer.MIN_VALUE;
}
return isPlus?result:-result;
}