首页 > 代码库 > String,StringBuilder的方法,伪随机Random类

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

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());            }}