1 package com.vince; 2 3 import java.util.Arrays; 4 5 public class StringDemo2 { 6 7 public static void main(String[] args) { 8 9 String str = " fkwefwfa d6737383 "; 10 char c = str.charAt(1); 11 System.out.println(c); 12 13 System.out.println(str.toCharArray()); 14 char[] cs = {'a','b','c'}; 15 String s1 = new String(cs); 16 17 String s2 = new String(cs,0,1); 18 System.out.println(s2); 19 20 System.out.println(Arrays.toString(str.getBytes())); 21 22 System.out.println(str.replace('w', '*')); 23 System.out.println(str.replaceAll("\\d", "*")); 24 25 System.out.println(str.substring(0, 4)); 26 27 System.out.println(Arrays.toString(str.split("d"))); 28 29 System.out.println(str.contains("a")); 30 31 System.out.println(str.indexOf("f")); 32 System.out.println(str.lastIndexOf("f")); 33 System.out.println(str.isEmpty()); 34 35 System.out.println(str.length()); 36 37 System.out.println(str.trim()); 38 39 System.out.println(str.concat("*****")); 40 System.out.println(String.valueOf(10)); 41 } 42 43 }
1 package com.vince; 2 3 /** 4 * 对象需要具备克隆功能: 5 * 1、实现Cloneable接口,(标记接口) 6 * 2、重写Object类中的clone方法 7 * @author vince 8 * @description 9 */ 10 public class Cat implements Cloneable{ 11 private String name; 12 private int age; 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 public int getAge() { 20 return age; 21 } 22 public void setAge(int age) { 23 this.age = age; 24 } 25 public Cat(String name, int age) { 26 super(); 27 this.name = name; 28 this.age = age; 29 } 30 public Cat() { 31 super(); 32 } 33 //重写Object中的clone方法 34 @Override 35 protected Object clone() throws CloneNotSupportedException { 36 return super.clone(); 37 } 38 @Override 39 public String toString() { 40 return "Cat [name=" + name + ", age=" + age + "]"; 41 } 42 43 }
1 package com.vince; 2 3 import java.io.IOException; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 9 Cat cat = new Cat("ίχίχΠʽ°Χ",2); 10 try { 11 Cat newCat = (Cat) cat.clone(); 12 13 14 System.out.println("cat="+cat); 15 System.out.println("new cat="+newCat); 16 System.out.println(cat==newCat); 17 } catch (CloneNotSupportedException e) { 18 e.printStackTrace(); 19 } 20 21 } 22 23 }