本章目标
掌握String类中的常用方法
了解API文档的查找方法

 

String类中的常用方法(1)

String类的常用方法_字符串


 String类中的常用方法(2)

String类的常用方法_常用方法_02


 

部分String类的常用方法如下:

public class StringAPIDemo01 {
	public static void main(String[] args) {
		String str1 = "hello";//定义字符串
		char c[] = str1.toCharArray();//将字符串变为字符数组
		for(int i=0; i<c.length; i++){//循环输出
			System.out.print(c[i]+"、");
		}
		System.out.println("");
		String str2 = new String(c);//将全部字符数组变为 String
		String str3 = new String(c, 0, 3);//将部分字符数组变为 String
		System.out.println(str2);//输出字符串
		System.out.println(str3);//输出字符串
	}
/*结果:
 * h、e、l、l、o、
 * hello
 * hel
 * */
}

 

public class StringAPIDemo02 {
	public static void main(String[] args) {
		String str1 = "hello";//声明 String 对象
		System.out.println(str1.charAt(3));//取出字符串第 4 个字符
	}
/*结果:
 * l
 * */
}

 

public class StringAPIDemo03 {
	public static void main(String[] args) {
		String str1 = "hello";//定义字符串
		byte b[] = str1.getBytes();//将字符串变为 byte 数组
		System.out.println(new String(b));//将全部 byte 数组变为字符串
		System.out.println(new String(b, 1, 3));//将部分 byte 数组变为字符串
	}
/*结果:
 * hello
 * ell
 * */
}

 

public class StringAPIDemo04 {
	public static void main(String[] args) {
		String str1 = "hello chaoyi!";//定义字符串变量
		System.out.println("\""+str1+"\"的长度为:"+str1.length());
	}
/*结果:
 * "hello chaoyi!"的长度为:13
 * */
}

 

public class StringAPIDemo05 {
	public static void main(String[] args) {
		String str1 = "abcdefgcgh";//声明字符串
		System.out.println(str1.indexOf("c"));//查到返回位置
		System.out.println(str1.indexOf("c",3));//查到返回位置,从第 4 个开始查找
		System.out.println(str1.indexOf("x"));//没有查到返回 -1
	}
/*结果:
 * 2
 * 7
 * -1
 * */
}

 

public class StringAPIDemo06 {
	public static void main(String[] args) {
		String str1 = "     hello       ";//定义字符串
		System.out.println(str1.trim());//去掉左右空格后输出
	}
/*结果:
 * hello
 * */
}

 

public class StringAPIDemo07 {
	public static void main(String[] args) {
		String str1 = "hello world";//定义字符串
		System.out.println(str1.substring(6));//从第 7 个位置开始截取
		System.out.println(str1.substring(0,5));//截取 0~5 个位置的内容
	}
/*结果:
 * world
 * hello
 * */
}

 

public class StringAPIDemo08 {
	public static void main(String[] args) {
		String str1 = "hello world";//定义字符串
		String s[] = str1.split(" ");//将空格进行字符的拆分
		for(int i=0; i<s.length; i++){//循环输出
			System.out.println(s[i]);
		}
	}
/*结果:
 * hello
 * world
 * */
}

 

public class StringAPIDemo09 {
	public static void main(String[] args) {
		System.out.println("将\"hello world\"转成大写:"+"hello world".toUpperCase());
		System.out.println("将\"HELLO WORLD\"转成大写:"+"HELLO WORLD".toLowerCase());
	}
/*结果:
 * 将"hello world"转成大写:HELLO WORLD
 * 将"HELLO WORLD"转成大写:hello world
 * */
}

 

public class StringAPIDemo10 {
	public static void main(String[] args) {
		String str1 = "**HELLO";//定义字符串
		String str2 = "HELLO**";//定义字符串
		if(str1.startsWith("**")){//判断是否以 “**”开头
			System.out.println("(**HELLO)是以 ** 开头");
		}
		if(str2.endsWith("**")){//判断是否以 “**”结尾
			System.out.println("(HELLO**)是以 ** 结尾");
		}
	}
/*结果:
 * (**HELLO)是以 ** 开头
 * (HELLO**)是以 ** 结尾
 * */
}

 

public class StringAPIDemo11 {
	public static void main(String[] args) {
		String str1 = "HELLO";//定义字符串
		String str2 = "hello";//定义字符串
		System.out.println("\"HELLO\".equals\"hello\" --> "+str1.equals(str2));
		//不区分大小写比较字符串内容
		System.out.println("\"HELLO\".equalsIgnoreCase\"hello\" --> "+str1.equalsIgnoreCase(str2));
	}
/*结果:
 * "HELLO".equals"hello" --> false
 * "HELLO".equalsIgnoreCase"hello" --> true
 * */
}

 

public class StringAPIDemo12 {
	public static void main(String[] args) {
		String str = "hello";//声明字符串
		String newStr = str.replaceAll("l","x" );//现在将所有的 I 替换成 x
		System.out.println("替换之后的结果:"+newStr);
	}
/*结果:
 * 替换之后的结果:hexxo
 * */
}