String 类

来源:互联网 发布:淘宝开放平台sdk下载 编辑:程序博客网 时间:2024/06/15 02:11
public static void main(String[] args){

String f=new String();   //无参构造器




String a=new String("abc");   


String b=new String("ab22cdf2g4h"); //有参的


String c=new String("abc");


char[] n={'2','5','6','8','2'};//  数组
String d=new String(n);//构造器 引用 数组


//System.out.println(d);
int i=d.length();//字符串个数 长度
System.out.println("1"+" "+i);


char c1=d.charAt(2);//指定位置 上的字符
System.out.println("2"+" "+c1);


int i1=d.indexOf('2');   //字符串有相同的 头一//位是
System.out.println("3"+" "+i1);

int i2=b.lastIndexOf("a");  //字符串有相同的 最后//一位是
System.out.println("4"+" "+i2);




boolean b1=b.startsWith("a");//比较字符串是否是开头
System.out.println("5"+" "+b1);


boolean b2=b.endsWith("h"); //比较字符串是否是结尾
System.out.println("6"+" "+b2);


System.out.println("7"+" "+f.isEmpty());  //判断字符串长度是否为0


String s1=b.substring(2,4);
System.out.println("8"+" "+s1);    //下标2截取到下标4之前的字符串


String s2=b.replace("22","55");
System.out.println("9"+" "+s2);  //字符串里面 字符 22 转换为55

String s3=b.replace('2','7');
System.out.println("10"+" "+s3);  //字符串里面 所有2 转换为 7




String s4=String.valueOf(88.6);
System.out.println("11"+" "+s4);  //基本数据类型转换为 字符串类型


int i3=a.compareTo(c);
System.out.println("12"+" "+i3);   // 构造器a的对象和c对象  字符串比较 大小 


boolean b3=a.equals(c);
System.out.println("13"+" "+b3); //  a的对象 字符串和c的字符串是否一样






//String s5=a+c;// 简写 拼接
String s5=a.concat(c);// 两个对象字符串拼接 
System.out.println("14"+" "+s5);


byte[] b4=a.getBytes();//字符串编码为byte   储存为新数组
System.out.println("15"+" "+b4[1]);




String[] s6=b.split("4");
System.out.println("16"+" "+s6[0]);
System.out.println("16"+" "+s6[1]);

}


byte[]---getBytes()     字符串变为字节数组

char[] ----tocharArray() 将字符串转换为一个新的字符数组

0 0
原创粉丝点击