Sting中的方法

1.StringBuffer

public class StringBuffer {
    public static void main(String[] args) {
        long start=System.currentTimeMillis();

        String str="hello";
        for( int i=0;i<100000;i++){
            str+="world";    // java +是重载的运算符, 相当于  string 中的   concat  ()   会生成新的字符串
        }

        long end=System.currentTimeMillis();

        System.out.println(   end-start );   // 21633
        long start1=System.currentTimeMillis();

        StringBuilder str1=new StringBuilder("hello");
        for( int i=0;i<100000;i++){
            str1.append( "world"   );    // java +是重载的运算符, 相当于  string 中的   concat  ()   会生成新的字符串
        }

        long end1=System.currentTimeMillis();

        System.out.println(  (end1-start)-(end-start)  );   // 8

        System.out.println(   str1.toString() );
    }
}

2.Class

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class StringClass {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class c=Class.forName("com.yc.language.Person");
        Field[] fs=c.getFields();             //共有属性
        Field[] fc=c.getDeclaredFields();     //只会有自己声明的属性
        for(Field s:fs){
            System.out.println(fs);
        }
        for(Field s:fc){
            System.out.println(fc);
        }
        Method[] m1=c.getDeclaredMethods();
        Method[] m2=c.getMethods();
        for(Method m:m2){
            System.out.println(m);
        }
        Object obj =  c.newInstance();
        System.out.println(obj);
        System.out.println(obj instanceof Person);
        Person p=new Person();
        System.out.println(p.getClass());
    }
}

3.比较

public class StringCompareTo {
    public static void main(String[] args) {
        String s10="chinap";
        String s11="chinese";
        System.out.println(   s10.compareTo(s11 )  );   //   -4
        s10="china";
        s11="chinapo";
        System.out.println(   s10.compareTo(s11 )  );   //

    }
}

4.添加

public class StringConcat {
    public static void main(String[] args) {
        String s="xxx";
        System.out.println(s.concat("   hello"));
    }
}

5.比较

public class StringEquals {
    public static void main(String[] args) {
        String s=new String();
        String s2=new String(new byte[]{97,98});
        String s3=new String(new char[]{'a','b'});
        String s5=new String(new char[]{'a','b','c'},1,2);//偏移量 1 取值 2个
        System.out.println(s+" "+s2+" "+s3+" "+s5);
        System.out.println(s2.hashCode());//object中的hashcode在子类中都重写,以保证不同内容输出不同hashcode
        s2="xx";
        System.out.println(s2.hashCode());
        System.out.println(s2.length());
        String s6="abc";
        String s7="abc";
        String s8=new String("abc");
        String s9=new String("abc");
        System.out.println(s6==s7);//比地址
        System.out.println(s7==s8);
        System.out.println(s9==s8);
        System.out.println(s6.equals(s7));//比内容
        System.out.println(s7.equals(s8));//字符串的equals是比较字符串的每个字节

    }
}

6.结束

public class StringExitTest {
    public static void main(String[] args) {
        System.out.println("xx");
        System.exit(0);
        System.out.println("xx");
    }
}

7.查找位置

public class StringIndexof {
    public static void main(String[] args) {
        String s="a.txt";
        System.out.println((int)'.');
        System.out.println(s.indexOf(46));
        System.out.println(s.indexOf("."));
        System.out.println(s.indexOf('.'));//隐式类型转换    char可以隐式转换成int char只有6万多字符,字符不够 int够
        System.out.println(s.lastIndexOf("."));
    }
}

8.替换

public class StringReplace {
    public static void main(String[] args) {
        String s="x.x.x.app";
        System.out.println(s.replace("x","y"));
        System.out.println(s.replace('.','*'));
        System.out.println(s.replace("app","add"));
    }
}

9.截取

public class StringSplit {
    public static void main(String[] args) {
        String s13="张三,男,23\n李四,女,22";   //截取
        String[] ss= s13.split("\n");
        for(  String str:ss){
            String[] person= str.split(",");
            for( String p:person){
                System.out.print(  p +"\t");
            }
            System.out.println();
        }
    }
}

10.截取

public class StringSubString {
    public static void main(String[] args) {
        String s="abcdefg";
        System.out.println(s.substring(1));//从位置1开始
        System.out.println(s.substring(2,5));//从位置2开始 截取到5-2个位置以后

    }
}
import java.io.UnsupportedEncodingException;

public class StringTo {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String s="abc张";
        System.out.println( s.toCharArray() instanceof char[] );
        System.out.println(   s.getBytes()    );
        for(   byte bb: s.getBytes()  ){
            System.out.println(  bb );
        }
        System.out.println(   s.getBytes("ISO-8859-1")   );    // 存的时候用变长        ->   utf8   gbk   gb2312  ISO-8859-1
        for(   byte bb: s.getBytes()  ){
            System.out.println(  bb );
        }
        // 存成 字节数组指定的编码要与取一致  ISO-8859-1没有中文 gbk和gb2312有转换关系
        byte[] bs= s.getBytes("gbk");
        System.out.println(     new String(  bs,"gbk") );
        System.out.println(     new String(  bs,"gb2312") );
        System.out.println(     new String(  bs,"ISO-8859-1") );
        byte[] bs1= s.getBytes("utf8");
        System.out.println(     new String(bs1,"utf8"));

    }
}

12.前后去空格

public class StringTrim {
    public static void main(String[] args) {
        String s=" x x x ";
        System.out.println(s.trim());  //去除前后空格 不去中间的
    }
}

13.判断

public class StringWith {
    public static void main(String[] args) {
        String s="hello world";
        System.out.println(s.startsWith("hello"));
        System.out.println(s.endsWith("world"));
        System.out.println(s.startsWith("hello",0));
        System.out.println(s.startsWith("world",6));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值