package com.test.test04; import java.util.Arrays; import java.util.Random; public class Test01 { //这是一个main方法,是程序的入口 public static void main(String[] args) { //声明变量,类型为String类型 String str = "abc"; System.out.println(str); String str2 = ""; //通过构造器来创建对象 String s1 = new String(); String s2 = new String("abc"); String s3 = new String(new char[]{'a','b','c'}); String s4 = "abc"; System.out.println("字符串长度"+s4.length()); //字符串长度3 String s5 = new String(); System.out.println("字符串是否为空"+s5.isEmpty()); //字符串是否为空true String s51 = new String("abc"); System.out.println("字符串是否为空"+s51.isEmpty()); //字符串是否为空false System.out.println("获取字符串的下标对应的字符为:"+s51.charAt(1)); //获取字符串的下标对应的字符为:b String s6 = new String("abc"); String s7 = new String("abc"); System.out.println(s6.equals(s7)); //true String s61 = new String("abc"); String s71 = new String("abe"); System.out.println(s61.equals(s71)); //false String s8 = new String("abc"); String s9 = new String("abc"); System.out.println(s8.compareTo(s9)); //0 String s81 = new String("abc"); String s91 = new String("abcdef"); System.out.println(s81.compareTo(s91)); //-3 String s82 = new String("abc"); String s92 = new String("accdef"); System.out.println(s82.compareTo(s92)); //-1 //字符串的截取 String s10 = "abcdefhijk"; System.out.println(s10.substring(3)); //defhijk System.out.println(s10.substring(3,6));//[3,6) def //字符串的合并/拼接操作 System.out.println(s10.concat("pppp")); //abcdefhijkpppp //字符串中的字符的替换 String s11 = "abcdefhija"; System.out.println(s11.replace('a','u'));//ubcdefhiju //按照指定的字符串进行分裂为数组的形式 String s12 = "a-b-c-d-e-f-h"; String[] strs = s12.split("-"); System.out.println(strs); //[Ljava.lang.String;@1b6d3586 //Arrays 该类包含用于操作数组的各种方法(如排序和搜索)。 该类还包含一个静态工厂,可以将数组视为列表。 //toString 返回指定数组的内容的字符串表示形式 System.out.println(Arrays.toString(strs)); //[a, b, c, d, e, f, h] //转大小写的方法 String s13 = "abc"; System.out.println(s13.toUpperCase()); //ABC System.out.println(s13.toUpperCase().toLowerCase()); //abc //去除首尾空格 String s14 = " a b c "; System.out.println(s14); // a b c System.out.println(s14.trim()); //a b c String s15 = "abc"; System.out.println(s15.toString()); //abc //转换为String类型 System.out.println(String.valueOf(false));//输出字符串false System.out.println(String.valueOf(123)); //输出字符串123 } }
04-16
251
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)