String应用杂记

来源:互联网 发布:淘宝网店怎么经营 编辑:程序博客网 时间:2024/06/18 08:53
package com.zhiyouztd;import java.util.Arrays;public class TestString extends Object {    public static void main(String[] args) {//      String s3 = "abc";//      String s4 = new String("abc");//      s3 = s4;//将s4的地址 赋值给s3的地址//      System.out.println(s3==s4);//比较的是地址//      System.out.println(s3.equals(s4));//先比较地址 再比较内容        //字符串常量池//      String s1 = "abc";//      String s2 = "abc";//      System.out.println(s1==s2);//      System.out.println(s1.equals(s2));//      String s1 = new String(); //创建String对象,字符串中没有内容//      byte[] bys = new byte[]{97,98,99,100};//数组//      String s2 = new String(bys); // 创建String对象,把数组元素作为字符串的内容//      System.out.println(s2);//-----abcd//      //      char[] char1 = new char[]{97,'b','c','a'};//数组//      String s3 = new String(char1); // 创建String对象,把数组元素作为字符串的内容//      System.out.println(s3);//-----abcd//      String s4 = new String(char1, 0, 2); //创建String对象,把一部分数组元素作为字符串的内容,参数offset为数组元素的起始索引位置,参数length为要几个元素//      System.out.println(s4);//      //        String s5 = "abc";//      String s6 = new String("abc"); //创建String对象,字符串内容为abc//      String a = "中国abc12334";//      //System.out.println(a.length());//      //      System.out.println(a.substring(0, 2));//中国 截取后的内容做为一个新的变量,不影响原来字符串的内容//      System.out.println(a);//中国abc12334//      System.out.println(a.substring(2));//abc12334//      //      //判断字符串是否以什么开头或结尾//      System.out.println(a.startsWith("中国"));//true//      System.out.println(a.startsWith("中国bc"));//false//      //      //先从下标为2的位置往后截取到字符串最后,然后判断截取后的新内容是否以abc开头//      System.out.println(a.startsWith("abc", 2));//true //      //1. a.substring(2)-->abc12334   2."abc12334".startsWith("abc")-->true//      // a.substring(2).startsWith("abc");//      System.out.println(a.endsWith("334"));//true//      //      //判断一个字符串中是否包含某个字符或字符串//      //      String a1 = "accca";//      System.out.println(a1.contains("落日"));//      //      //返回指定子字符串在此字符串中第一次出现处的索引。//      System.out.println(a1.indexOf("cc"));//参数为int的时候是ascii码值//      //      //返回索引所在位置的字符内容//      System.out.println(a1.charAt(4));//      String a1 = "acccaafasdfdsafdsa";//      //      //将字符串转换为字符数组//      char[] ch1 = a1.toCharArray();//      //char[] str = new char[3];//???????//      System.out.println(ch1);//      for(int i=0;i<ch1.length;i++) {//          System.out.println(ch1[i]);//      }//      //      //返回字符最后一次出现的位置//      System.out.println(a1.lastIndexOf("d"));//      //      String a1 = "  大  漠  ";//      String newStr = a1.replace("大漠", "长河");//      System.out.println(a1);//        System.out.println(newStr);//        System.out.println(a1.length());//---16//        System.out.println(a1.trim().length());//13        String a2 = "ab-cd-Ac-B";//        System.out.println(a2.toUpperCase());//        System.out.println(a2.toLowerCase());//        //        //将其它类型的数据转换为字符串//        System.out.println(String.valueOf(123));        String[]  c1 = a2.split("-");        System.out.println(Arrays.toString(c1));    }}