码迷,mamicode.com
首页 > 其他好文 > 详细

String,StringBuilder的方法,伪随机Random类

时间:2014-08-08 01:48:55      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   io   for   ar   div   

import java.nio.charset.Charset;

public class StringDemo {
    public static void main(String[] args) {
        byte[]bt={‘a‘,‘b‘,‘c‘};
//        String(byte[]bt,Charset charset);
//        该构造方法根据给定的byte数组和字符集组成一个新的String对象
        Charset charset=Charset.forName("utf-8");
        String s1=new String(bt,charset);
        System.out.println(s1);
        System.out.println(s1.charAt(1));
        String s2="abcde";
//        s2的前半部分是s1,compareTo方法返回两个字符串的长度差
        System.out.println(s1.compareTo(s2));
//        s1,s3从0(a)开始比较,返回第一个不相等的字符差(b与a之差)
        String s3="aac";
        System.out.println(s1.compareTo(s3));
//        concat字符串连接,与+作用相同
        System.out.println(s1.concat(s2));
        System.out.println(s1.concat(s2).endsWith(s2));
        System.out.println(s1.equalsIgnoreCase("ABC"));
        byte[]bb=s1.getBytes();
        char []ch=new char[s1.length()-1];
        System.out.println((char)bb[1]);
        s1.getChars(0, s1.length()-1,ch, 0);
        System.out.println(s1.replace(‘a‘, ‘A‘));
        System.out.println(s1);
    }

}

StringBuilder

public class BuilderDemo {
    public static void main(String[] args) {
        StringBuilder sb=new StringBuilder();
        sb.append(" here ");
        sb.append("am ");
        sb.append("I ");
        System.out.println(sb);
        sb.insert(5," here");
        System.out.println(sb);
        System.out.println(sb.reverse());
        sb.delete(2,3);
        sb.setLength(5);
        System.out.println(sb.length());
        System.out.println(sb.capacity());
    }
}
public class RandDemo {
    public static void main(String[] args) {
        Random rand=new Random(100);
        System.out.println(rand.nextBoolean());
        System.out.println(rand.nextInt());
        Random rand2=new Random(100);
//        rand2 rand的运行结果一样
        System.out.println(rand2.nextInt());
        System.out.println(rand2.nextBoolean());
        
    }

}

 

String,StringBuilder的方法,伪随机Random类,布布扣,bubuko.com

String,StringBuilder的方法,伪随机Random类

标签:style   blog   color   java   io   for   ar   div   

原文地址:http://www.cnblogs.com/twobrothers/p/3898280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!