package com.hanqi; import java.util.Random; public class Text { public static void main(String[] args) { String str1 = "字符串常量";// 常用 String str2 = null; str2 = new String(); str2 = new String("实例化字符串"); char[] c = new char[] { 'a', 'b', 'c' }; str2 = new String(c); str2= "abcdefghigklmnopqrstuvwsyzd"; //字符集 //str2 = new String(bytes) System.out.println("str2.length=" + str2.length()); System.out.println("str2=" + str2); //查找字符或字符串 int in = str2.indexOf("a"); System.out.println("a="+ in);//按索引值算 int la=str2.lastIndexOf("d"); System.out.println("d="+ la); String newStr=str2.substring(5); newStr=str2.substring(5, 9);//不包含结束位置,不能超出结束索引 System.out.println("sudstring()="+newStr); str2 = " a b c s g b j "; //去除前后空格 System.out.println("去空格="+str2.trim()+"后面"); //查找替换 System.out.println("查找替换空格="+str2.replace(" ", "")+"后面"); str2 = "abc,你好,abcd"; System.out.println("查找替换="+str2.replaceFirst("abc", "张三")+""); str2= "abcdefg"; //判断字符串的开始和结束 System.out.println("判断起始="+str2.startsWith("acb")); System.out.println("判断起始="+(str2.indexOf("acb")==0)); System.out.println("判断结束="+str2.endsWith("fg")); str1 = "abc";//new String("abc"); str2 = "abc";//new String("abc"); str2 = "def"; System.out.println("判断字符串相等="+(str1==str2));//==是比较指针的地址是否相等 System.out.println("判断字符串相等="+(str1==str2)+"str1="+str1.toUpperCase()+"shr2="+str2.toLowerCase()); System.out.println("判断字符串相等="+str1.equals(str2)); str2 = "abc#def#ghr#xyz"; String[] array = str2.split("#"); for(int i=0;i<array.length;i++) { System.out.println("数组="+array[i]); } //判断结束 是不是用q结束 str2 ="asjhfiughwjfhj9e8fajkdi09faskdhhjq"; String st = new String("q"); System.out.println("最后以为是q="+str2.substring(str2.length()-st.length()).equals(st)); //数学运算 System.out.println("四舍五入"+Math.round(123.556)); //取上限值 大于或等于它的最小整数 System.out.println("取上限值"+Math.ceil(123.456)); //取下限值 小于或等于它的最大整数 System.out.println("取上限值"+Math.floor(123.456)); //π System.out.println("PI="+Math.PI); //取随机数 System.out.println("随机数="+Math.random()); System.out.println("随机数="+Math.random()); System.out.println("随机数="+Math.random()); System.out.println("随机数="+Math.random()); System.out.println("随机数="+Math.random()); System.out.println("随机数="+Math.random()); System.out.println("随机数="+Math.random()); Random r = new Random();//用时间做种子 //r = new Random(1);//随机数种子 System.out.println("random随机数="+r.nextInt(1000)); System.out.println("random随机数="+r.nextInt(1000)); System.out.println("random随机数="+r.nextInt(100)); System.out.println("random随机数="+r.nextInt(100)); System.out.println("random随机数="+r.nextInt(100)); System.out.println("random随机数="+r.nextInt(100)); System.out.println("random随机数="+r.nextInt(100)); System.out.println("random随机数="+r.nextInt(100)); } }