String类和StringBuffer

## String类的初始化

    public class Example01 {
     public static void main(String[] args){
      String str1 = new String();                  //创建一个空的字符串
      String str2 = new String("abcd");            //创建一个内容为abcd的字符串
      char[] charArray = new char[]{'A','B','C'};  //创建一个内容为字符数组的字符串
      String str3 = new String(charArray);
      System.out.println("a" + str1 + "b");
      System.out.println(str2);
      System.out.println(str3);
     }
     }

*字符串的基本操作*
  char charAt(int index);
    int indexOf(String str);
    int lastIndexOf(String str);
    public class Example01{
     public static void main(String[] args){
      String s = "ababcdedcda";//声明字符串
      System.out.println("字符串的长度为:" + s.length());
      System.out.println("字符串中第一个字符:" + s.charAt(0));
      System.out.println("字符c第一次出现的位置:" + s.indexOf('c'));
      System.out.println("字符c最后一次出现的位置:" + s.lastIndexOf('c'));
      System.out.println("子字符串第一次出现的位置:" + s.indexOf("ab"));
      System.out.println("子字符串最后一次出现的位置:" + s.lastIndexOf("ab"));
     }
    }

*字符串的转换操作*
 char[] toCharArray()
    String toUpperCase()
    static String valueOf(int i) 
    public class Example01{
     public static void main(String[] args){
      String str = "abcd";
      System.out.print("将字符串转换为字符数组后的结果:");
      char[] charArray = str.toCharArray();//字符串转换为字符数组
      for(int i = 0;i<charArray.length;i++){
       if(i != charArray.length - 1){
        //如果不是数组的最后一个元素,在元素后加上逗号
        System.out.print(charArray[i] + ","); 
       }else{
        System.out.println(charArray[i]);
       }
      }
      System.out.println("将int值转换为String类型之后的结果:" + String.valueOf(12));
      System.out.println("将字符串转换成大写之后的结果:" + str.toUpperCase());
     }
    }

*字符串的替换和去除空格操作*
 String replace(CharSequence oldstr,CharSequence newstr);
    String trim(); 去除原字符串首尾空格
    public class Example01{
     public static void main(String[] args){
      String s = "itcast";
      //字符串替换操作
      System.out.println("将it替换成cn.it的结果:" + s.replace("it", "cn.it"));
      //字符串去除空格操作
      String s1 = "    i t c a s t    ";
      System.out.println("去除字符串两端空格后的结果:" + s1.trim());
      System.out.println("去除字符串中所有空格后的结果:" + s1.replace(" ",""));
    }
    }

字符串的判断操作(true or false)

// boolean equals(Object anObject)
    //boolean isEmpty()
    //boolean startsWith(String prefix)
    //boolean contains(CharSequence cs)
    public class Example01{
     public static void main(String[] args){
      String s1 = "String";
      String s2 = "Str";
      System.out.println("判断是否以字串Str开头:" + s1.startsWith("Str"));
      System.out.println("判断是否以字符串ng结尾:" + s1.endsWith("ng"));
      System.out.println("判断是否包含字符串tri:" + s1.contains("tri"));
      System.out.println("判断字符串是否为空:" + s1.isEmpty());
      System.out.println("判断两个字符串是否相等" + s1.equals(s2));
     }
    }
  

      
    
 


*字符串的截取和分割*

        public class Example01{
     public static void main(String[] args){
      String str = "羽毛球-篮球-兵乓球";
      System.out.println("从第5个字符截取到末尾的结果:" + str.substring(4));
      System.out.println("从第5个字符截取到第6个字符的结果:" + str.substring(4,6));
      System.out.println("分割后的字符串数组中的元素依次为:");
      String[] strArray = str.split("-");
      for(int i = 0;i<strArray.length;i++){
       if(i != strArray.length - 1){
        System.out.println(strArray[i] + ",");
       }else{
        System.out.println(strArray[i]);
       }
      }
     }
    }

*StringBuffer类*
//StringBuffer类与String最大的区别在于内容和长度可修改
public class Example01{
 public static void main(String[] args){
StringBuffer sb1 = new StringBuffer("abc");
  System.out.println("1,添加——————————————————————");
  add();
  System.out.println("1,添加——————————————————————");
  remove();
  System.out.println("1,添加——————————————————————");
  alter();
 }
 public static void add(){
  StringBuffer sb = new StringBuffer();
  sb.append("abcdefg");
  System.out.println("append添加结果:" + sb);
  sb.insert(2, "123");
  System.out.println("insert添加结果:" + sb);
 }
 public static void remove(){
  StringBuffer sb = new StringBuffer("abcdefg");
  sb.delete(1, 5);
  System.out.println("删除指定结果:" + sb);
  sb.deleteCharAt(2);
  System.out.println("删除指定结果:" + sb);
  sb.delete(0, sb.length());//清空缓冲区
  System.out.println("清空缓冲区结果:" + sb);
 }
 public static void alter(){
  StringBuffer sb = new StringBuffer("abcdefg");
  sb.setCharAt(1, 'p');//修改指定位置字符
  System.out.println("修改指定位置字符结果:" + sb);
  sb.replace(1, 3, "qq");//替换指定位置字符串或字符
  System.out.println("替换指定位置字符(串)" + sb);
  System.out.println("字符串翻转结果:" + sb.reverse());
 }
}

注意事项!!!
1:String类覆盖了Object类的equals()方法,而StringBuffer类没有
2:String类对象可以用操作符"+"进行连接,而StringBuffer类对象之间不能
例1:

String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1.equals(s2));//打印结果是true
StringBuffer sb1 = new StringBuffer("abc");
StringBuffer sb2 = new StringBuffer("abc");
System.out.println(sb1.equals(sb2));//打印结果是false



public class Example01{
        public static void main(String[] args){
    String s1 = "a";
    String s2 = "b";
    String s3 = s1 + s2;
    System.out.println(s3); //打印结果是ab;
    //StringBuffer sb1 = new StringBuffer("a");
    //StringBuffer sb1 = new StringBuffer("b");
    //StringBuffer sb3 = sb1 + sb2;        //编译出错
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_43255303/article/details/84098403