您的位置:首页 > 其它

String字符串与整数之间的相互转换

2016-03-01 20:50 357 查看
1.数字转换成字符串

int n = 2016

String str = String.valueOf(n);

2.字符串转换成数字

例题:比较IP大小,比如192.168.1.1 > 192.168.0.255 

import org.junit.Test;

public class StringToInt {
public void demo1(int n){

}
/**
* 如果IP1 > IP2 返回ture 否则返回false
*/
public boolean demo2(String ip1,String ip2){
String[] str1 = ip1.split("\\.");
String[] str2 = ip2.split("\\.");
int length = str1.length;
System.out.println(length);

int m[] = new int[length];
int n[] = new int[length];
for(int i=0;i<length;i++){
m[i] = Integer.parseInt(str1[i]);
n[i] = Integer.parseInt(str2[i]);
}

for(int i=0;i<length;i++){
if(m[i] > n[i]){
return true;
}
}
return false;
}

@Test
public void testDemo1(){
String s = "192.168.1.1";
String s1 = "192.167.1.1";
boolean result = demo2(s,s1);
System.out.println(result);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: