您的位置:首页 > 其它

Character,String相关方法,Int,double互相转换

2012-10-19 10:55 477 查看
/*
*
* String相关方法
*
*/
public class StringTest2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String a ="abc123";
String b ="Abc123";

if(a.equalsIgnoreCase(b)){
System.out.println("两个对象内容相等");
}else{
System.out.println("两个对象内容不相等");
}

int iii = b.compareTo(a); //逐个比较字符串,如果发现一模一样返回零,当发现一个不一样的时候,就返回两个值
//ascii码的差,并停止比较
System.out.println(iii);

String url ="http://www.163.com";
//判断是否以指定字符串开头,指定字符结尾
if(url.startsWith("http") && url.endsWith("com")){
System.out.println("ok");
}else{
System.out.println("bu ok");
}

//分割字符串
//substring ,split
String sql = "select * from abc";
//得到select
System.out.println(sql.substring(0,6));
System.out.println(sql.substring(7));
//使用空格来区分
String[] aaa = sql.split(" ");

for(String str : aaa){
System.out.println(str);
}

System.out.println(sql.charAt(1));//得到指定索引位的字符
System.out.println(sql.indexOf('e'));

String s = sql.replaceAll("e", "a");
System.out.println(s);
}

}/*
*
*   包装类...承基本类型启String
*   Char包装类有很多字符的操作
*   String的不变性,字符串常量区的存在
*   String相加使用StringBuffer
*       StringBuffer sb = new StringBuffer(25000);//这个是默认创建一个可以存放16个字符的sb,这里创建了25000
*      sb.append("字符串").append("字符串");
*      字符串加完之后 String s = sb.toString();
*
*   String相关的方法:对字符串的操作
*
*/


public class CharacterTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

char c = 'a';

if(Character.isDigit(c)){
System.out.println("是一个数字");
}
if(Character.isLetter(c)){
System.out.println("是一个字母");
}
}
}


public class DoubleTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//double 和 Double

//        int i = 5;
//        Integer ii = new Integer(5);
//
double d = 12.34;

Double dou = new Double(d);
Double dou1 = Double.valueOf(d);
//int iii = ii.intValue();
double d1 = dou.doubleValue();

//double和String之间
//        String s  = Integer.toString(i);
//        System.out.println(s);
String s = Double.toString(d);

// int iiii = Integer.parseInt(s);
double d2 = Double.parseDouble(s);
//
//       //String 和Integer的关系处理
//         //1.String转Integer
//         Integer iiiii = Integer.valueOf(s);
//         System.out.println(iiiii);
//
//         //2.Integer转String
//         String ss = iiiii.toString();
Double d4 = Double.valueOf(s);
String ss = d4.toString();
}

}


public class IntegerTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//int 和 integer(int 的包装类)之间的转化

//1. int转integer
int i = 5;
Integer ii = new Integer(5);

//2. integer转int

int iii = ii.intValue();
System.out.println(iii);

//int和String之间的转换
//1. int转String
String s  = Integer.toString(i);
System.out.println(s);
//2.String转int
int iiii = Integer.parseInt(s);

//String 和Integer的关系处理
//1.String转Integer
Integer iiiii = Integer.valueOf(s);
System.out.println(iiiii);

//2.Integer转String
String ss = iiiii.toString();
}

}


import java.lang.reflect.Array;
import java.util.Arrays;

public class StringTest {

public static void main(String[] args) {

String s = "abccd";
String s0 = new String();//不要使用NEW来创建字符串

System.out.println(s.length());

IntegerTest it = new IntegerTest();
IntegerTest it1 = new IntegerTest();

//        String s1 = "abccd";
//        if(it == it1){
//            System.out.println("相等");
//        }
//
//        if(s == s1){
//
//            System.out.println("相等");
//
//
//        }
int a = 4;
int b = 4;
System.out.println(a==b);

int a1[] = new int[]{1,2,3};
int b1[] = new int[]{1,2,3};
System.out.println(a1==b1);
System.out.println(Arrays.equals(a1, b1));//数组equals比较奇怪

String a2 = new String("abcdefg");//是new 出来的不会放到字符串常量区
String b2 = new String("abcdefg");

System.out.println(a2==b2);
System.out.println(a2.equals(b2));//标准 equals调用

String a3 = "abcdefgh";
String b3 = "abcdefgh";
System.out.println(a3==b3);
System.out.println(a3.equals(b3));//标准 equals调用

//String的"bug"特性
//new 就会在内存的堆开地盘
//当我们以String a3 = "abcdefgh"创建字符串的时候
//以String a3 = 这种方式创建 时,JVM会检查字符串常量区是否有"abcdefgh",这个字符串,
//如果没有就创建一个放在里面,如果有的话,就将a3指向它

//应为这个特性带来的麻烦:那就是相加之后的字符串,并不是在原来字符串基础上变化,而是保留原来的字符串
//创建一个新的字符串,然后指向变量,
//这样子的话如果大量的相加操作出现就在常量区中创建非常多的无用字符串,内存上是种浪费

//String这样的特性原本就是为了节省内存,比如一个字符串有100万字符,现在又有一个相同100万字符,
//有了这个特性,我只要存一个字符串就可以
//所以有一个解决字符串相加的方案,就是使用一个叫做StringBuffer对象
String a4 = "abcdefg"+"123456";
a4 = a4+"aaa";

/*   for(int i =0 ;i<10000;i++){

a4 = a4+"a";
}这段代码会产生10001个字符串,10000个无用的*/

StringBuffer sb = new StringBuffer(a4);
for(int i = 0 ;i <10000 ;i++){
sb.append("a").append("1").append("222b").append("1233333fff");
}//只会产生一个sb对象
a4 = sb.toString();//将结果给a4
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: