字符串类的基本用法

/**
测试字符串类的基本用法
*/

public class TestString {
  
  public static void main(String[] args) {
      String str1 = "abc";
      String str2 = new String("def");
      String str3 = "abc" + "defgh";
      String str4 = "18" + 19;    //这不是加法,是连接符
      
      System.out.println(str4);   //1819
      
      String str10 = "abcc";
      String str11 = "abcc";
      String str12 = new String("abcc");
      
      System.out.println(str10 == str11);//true
      System.out.println(str12 == str11); //false,str11和str12不同对象
      //字符串对比需要使用equals
      System.out.println(str12.equals(str11));//true
     
      String s1 = "core Java";
      String s2 = "Core Java";
      
      System.out.println(s1.charAt(3));   //e,提取下标为3的字符
      System.out.println(s2.length());    //9,字符串的长度
      System.out.println(s1.equals(s2));  //false,比较两个字符串是否相等
      System.out.println(s1.indexOf("Java")); //5,字符串s1中是否包含Java
      System.out.println(s1.indexOf("apple"));    //-1,字符串s1中是否包含apple
      
      String s = s1.replace(‘ ‘,‘&‘); //将s1的空格替换成&
      System.out.println("result is :"+s);
      System.out.println(s1);     //不改变s1本身
      
      String ss1 = "";
      String ss2 = "How are you";
      
      System.out.println(ss1.startsWith("How"));    //是否以How开头
      System.out.println(ss1.endsWith("you"));     //是否以you结尾
      ss1=ss2.substring(4);//提取字符串:从下标为4的开始到字符串结尾为止
      System.out.println(ss1);//are you
      
      //String sss3 = "How old are you?";
      //ss1 = s2.trim();        //去除字符串首尾的空格
      
      //ss1 = ss3.toLowerCase();//转小写
      //ss1 = ss3.toUpperCase();//转大写
      }
  
}

字符串类的基本用法

上一篇:mysql查看数据库真实执行sql语句


下一篇:在centos7中安装mysql57