public class Learn {
public static void main(String[] args) {
char a[]={'A','B','C'};
String s=new String(a);//实现把一个字符数组转化成字符串
System.out.println(s);
/*包装类*/
/*基本数据类型不具有对象的特性,不能满足某些特殊的要求,
我们需要某些方法来处理基本类型的数据,所以需要包装类,包装类的父亲是Number类*/
//包装类的应用
int i=Integer.parseInt("1");//将一个字符串转换为int类型
System.out.println(i);
double j=Double.parseDouble("12");//将一个字符串转换为double类型
System.out.println(j);
double h=new Integer("56789").doubleValue();
System.out.println(h);
Integer h1=new Integer("56789").intValue();
System.out.println(h1.toString());
//根据对应关系
//装箱:把低级(基本)的自动转化为高级(引用)的
//拆箱:把高级(引用)的自动转化为低级(基本)的
Integer l1=10;//不装不拆??????????
int l2=l1;//拆箱
Integer l3=10;
System.out.println(l3+10);//根据低级走,先拆箱再运算
System.out.println(l3++);//拆箱
Boolean l4=true;
Boolean l5=false;
System.out.println(l4&&false);//拆箱
System.out.println(l4&&l5);
int l6=7;
Integer l7=l6;//装箱
Number l8=250.0f;//250.0会先被自动装箱为Float,然后指定给number
System.out.println(l1.doubleValue()/4);//拆箱
//其他类型转化为字符串类型
int y1=8;
String y2=String.valueOf(y1);
System.out.println(y2);
double y3=8.88;
String y4=String.valueOf(y3);
System.out.println(y4);
System.out.println(new Date().toString());//如果重写了,这是转变字符串的最快的方式
//比较相等
int f1=100;
int f2=101;
System.out.println(i==j);
String f3="hahaha";
String f4="hahaha";//String 值一旦相同,指向同一个堆
String f5=new String("hahaha");//强制开堆
System.out.println(f3==f4);//true
System.out.println(f4==f5);//false比较的是地址
System.out.println(f4.equals(f5));//true,比较的是内容是否相等
String f6="HAHAHA";
System.out.println(f3.equalsIgnoreCase(f6));//true,忽略大小写比较内容是否相等
//compareTo方法应用
System.out.println("ABC".compareTo("ABE"));//结果为-2
System.out.println("ABCde".compareTo("ABc"));//按照字典序的顺序比较
//String类型常用方法
/*concat,valueof,equals,equalsIgnoreCase,compareTo,startsWith,
endsWith,substring,spilt,trim*/
System.out.println(("woshilyf~").startsWith("wo"));
System.out.println(("woshilyf~").startsWith("lyf~"));
System.out.println(("woshilyf~").indexOf("wo"));//找的到返回索引,找不到返回-1
System.out.println(("woshilyf~woshilyf~").indexOf("wo",2));//从索引2向后找,返回目标的索引
System.out.println(("woshilyf~woshilyf~").lastIndexOf("lyf"));
System.out.println(("woshilyf~woshilyf~").lastIndexOf("lyf",5));//从索引16开始往前找,返回目标的索引,搭边就算
System.out.println(" C A ".trim()+"B"); //trim()去掉左右空格
//去掉中间空格的????????
String ch="hello worldhe lloworld";
int x;
StringBuffer ch1=new StringBuffer();
for(x=0;x<ch.length();x++){
char y=ch.charAt(x);
//System.out.
// print(y);
if(y!=' '){
ch1.append(y);
}
}
System.out.println(ch1);
String p="helloWorld";
System.out.println(p.substring(0,4));//截取索引【0~4)
String ss[]="a=b=c=d".split("=");
System.out.println(ss[0]+"--"+ss[1]+"--"+ss[2]+"--"+ss[3]);
String ll[]="haha%ni/hao".split("%");//分隔符,分割后形成一个数组
System.out.println(ll[0]+"---"+ll[1].split("/")[1]);
}
}
Java字符串
最新推荐文章于 2023-07-26 16:40:48 发布