String类概述
测试类:
package com.ax.test;
public class TestCreateStringObject {
public static void main(String[] args) {
String dec = "喜羊羊";
String newDec = new String(dec);
System.out.println("是否具有相等的字符序列:"+(dec.equals(newDec)));//true
System.out.println("是否具有同一个引用:"+(dec == newDec));//false
}
}
String类常用方法1
public char charAt(int index)
public int compareTo(String target)
public boolean equals(String target)
public boolean equalslgnoreCase(String target)
public boolean endsWith(String suffix)
public boolean startsWith(String prefix)
package com.ax.test;
public class TestStringMethods {
public static void main(String[] args) {
String name = "懒羊羊";
//charAt 方法,给定的索引不能超出字符串序列索引,否则出现异常
char res = name.charAt(2);
System.out.println(res);
//compareTo 方法,如果当前面对象大于后面对象则返回正数,小于则返回负数,等于返回0
String a = "abcdefg";
String b = "abceefg";
System.out.println(a.compareTo(b));
//测试是否以某个字符串正则表达式所描述的为开头或结尾的方法
String email = new String("abc@163.com");
System.out.println(email+"是否以.com结尾的"+email.endsWith(".com"));
}
}
String类常用方法2
public String trim()
public int indexOf(int ch)
public int indexOf(int ch,int fromIndex)
public int indexOf(String target)
public int indexOf(String target,int fromIndex)
public int length()
package com.ax.test;
public class TestStringMethods {
public static void main(String[] args) {
String name = "懒羊羊";
//charAt 方法,给定的索引不能超出字符串序列索引,否则出现异常
char res = name.charAt(2);
System.out.println(res);//羊
//compareTo 方法,如果当前面对象大于后面对象则返回正数,小于则返回负数,等于返回0
String a = "abcdefg";
String b = "abceefg";
System.out.println(a.compareTo(b));//-1
//测试是否以某个字符串正则表达式所描述的为开头或结尾的方法
String email = new String("abc@163.com");
System.out.println(email+"是否以.com结尾的"+email.endsWith(".com"));//abc@163.com是否以.com结尾的true
//去掉空格的trim方法
String srcStr = " 懒羊羊";
System.out.println(srcStr.length());//7
String newSrcStr = srcStr.trim();
System.out.println("新字符串:"+newSrcStr);//新字符串:懒羊羊
System.out.println("原字符串:"+srcStr);//原字符串: 懒羊羊
//String对象的indexOf查找目标字符或字符串方法
int index = email.indexOf('@');
System.out.println("@符号的位置索引:"+index);//3
}
}
String类常用方法3
public String[] split(String regex)
public String substring(int beginIndex)
public String substring(int beginIndex,int endIndex)
public char[] toCharArray()
public String toLowerCase()
public String toUpperCase()
package com.ax.test;
import java.util.Locale;
public class TestStringMethods1 {
public static void main(String[] args) {
//字符串的分割方法
String sheep = "懒羊羊,喜羊羊,美羊羊,沸羊羊,暖羊羊";
String [] sheepArray = sheep.split("喜羊羊");
System.out.println(sheepArray.length);//2
//提取字符串的sustring方法,注意不能超出原串的索引
String str1 = new String("我是一只快乐的小羊");
System.out.println(str1.substring(3));//只快乐的小羊
System.out.println(str1.substring(0,4));//我是一只
//将字符串转化为char型数组的tocharArray
char [] chars = "懒羊羊,喜羊羊,美羊羊,沸羊羊,暖羊羊".toCharArray();
System.out.println(chars.length == "懒羊羊,喜羊羊,美羊羊,沸羊羊,暖羊羊".length());//true
System.out.println("abcdefg".toUpperCase());//转换为大写ABCDEFG
System.out.println("abCDefg".toLowerCase());//转换为小写abcdefg
//字符串包含方法
String name = "喜羊羊,懒羊羊";
System.out.println(name.contains("懒羊羊"));//true
}
}
String类常用方法4
public String replace(char oldChar,char newChar)
public String replace(String oldChar,String newChar)
public static String valueOf(Object obj)
实体类:
package com.ax.pojo;
public class Users {
public String name ;
public Users(String name) {
this.name = name;
}
}
测试类:
package com.ax.test;
import com.ax.pojo.Users;
import java.util.Locale;
public class TestStringMethods1 {
public static void main(String[] args) {
//字符串的分割方法
String sheep = "懒羊羊,喜羊羊,美羊羊,沸羊羊,暖羊羊";
String [] sheepArray = sheep.split("喜羊羊");
System.out.println(sheepArray.length);//2
//提取字符串的sustring方法,注意不能超出原串的索引
String str1 = new String("我是一只快乐的小羊");
System.out.println(str1.substring(3));//只快乐的小羊
System.out.println(str1.substring(0,4));//我是一只
//将字符串转化为char型数组的tocharArray
char [] chars = "懒羊羊,喜羊羊,美羊羊,沸羊羊,暖羊羊".toCharArray();
System.out.println(chars.length == "懒羊羊,喜羊羊,美羊羊,沸羊羊,暖羊羊".length());//true
System.out.println("abcdefg".toUpperCase());//转换为大写ABCDEFG
System.out.println("abCDefg".toLowerCase());//转换为小写abcdefg
//字符串包含方法
String name = "喜羊羊,懒羊羊";
System.out.println(name.contains("懒羊羊"));//true
//字符串替换方法
String color = "blue";
System.out.println(color.replace("u","l"));//blle
System.out.println(color.replace("blue","yellow"));//yellow
//静态方法构建字符串对象
String dest = String.valueOf(new Users("喜羊羊"));
System.out.println(dest);//com.ax.pojo.Users@16d3586
System.out.println(String.valueOf(new Boolean(true)));//true
}
}