Java基础_0308:String类的常用方法

取出指定索引的字符 —— 使用charAt()方法
public class StringDemo {
	public static void main(String args[]) {
		String str = "hello";		// 定义字符串对象
		char c = str.charAt(0);		// 截取第一个字符
		System.out.println(c);		// 输出字符
	}
}
程序执行结果:
h

字符数组与字符串的转换
public class StringDemo {
	public static void main(String args[]) {
		String str = "hello";		// 定义字符串
		char[] data = str.toCharArray(); // 将字符串变为字符数组
		for (int x = 0; x < data.length; x++) {	// 循环输出每一个字符
			System.out.print(data[x] + "、");
		}
	}
}
程序执行结果:	h、e、l、l、o、
将字符串转大写
public class StringDemo {
	public static void main(String args[]) {
		String str = "hello“;		// 字符串由小写字母组成
		char[] data = str.toCharArray(); 	// 将字符串变为字符数组
		for (int x = 0; x < data.length; x++) {	// 改变每一个字符的编码值
			data[x] -= 32;
		}
		System.out.println(new String(data)); 	// 将全部字符数组变为String
		System.out.println(new String(data, 1, 2)); // 将部分字符数组变为String
	}
}
程序执行结果:
HELLO
EL



给定一个字符串,要求判断其是否由数字组成。
public class StringDemo {
	public static void main(String args[]) {
		String str = "123423432";
		if (isNumber(str)) {
			System.out.println("字符串由数字组成!");
		} else {
			System.out.println("字符串由非数字组成!");
		}
	}
	public static boolean isNumber(String temp) {
		char[] data = temp.toCharArray();// 将字符串变为字符数组,可以取出每一位字符进行判断
		for (int x = 0; x < data.length; x++) {// 循环判断
			if (data[x] > '9' || data[x] < '0') {// 不是数字字符范围
				return false;// 后续不再判断
			}
		}
		return true;	// 如果全部验证通过返回true
	}
}
程序执行结果:	字符串由数字组成!
观察字符串与字节数组的转换
public class StringDemo {
	public static void main(String args[]) {
		String str = "helloworld";	// 定义字符串
		byte[] data = str.getBytes(); 	// 将字符串变为字节数组
		for (int x = 0; x < data.length; x++) {
			data[x] -= 32; 	// 将小写字母变为大写形式
		}
		System.out.println(new String(data)); // 全部转换
		System.out.println(new String(data, 5, 5));// 部分转换
	}
}
程序执行结果:
HELLOWORLD(“new String(data)”语句执行结果)
WORLD(“new String(data, 5, 5)”语句执行结果)

相等判断
public class StringDemo {
	public static void main(String args[]) {
		String stra = "Hello"; // 实例化字符串对象
		String strb = "hELLO"; // 实例化字符串对象
		System.out.println(stra.equals(strb));  // 比较结果:false
		System.out.println(stra.equalsIgnoreCase(strb));  // 比较结果:true
	}
}
程序执行结果:
false(“stra.equals(strb)”语句执行结果)
true(“stra.equalsIgnoreCase(strb)”语句执行结果)

观察compareTo()方法
public class StringDemo {
	public static void main(String args[]) {
		String stra = "Hello";	// 定义字符串对象
		String strb = "HEllo";	// 定义字符串对象
		System.out.println(stra.compareTo(strb)); // 32,大于0
		if (stra.compareTo(strb) > 0) {// 可以利用大小等于0的方式来判断大小
			System.out.println("大于");
		}
	}
}
程序执行结果:
32(“stra.compareTo(strb)”语句执行结果,比较的是两个字符串的编码数值)
大于(“System.out.println("大于");”语句执行结果)


使用indexOf()等功能查找
public class StringDemo {
	public static void main(String args[]) {
		String str = "helloworld";		// 实例化字符串对象
		System.out.println(str.indexOf("world"));	// 返回满足条件单词的第一个字母的索引
		System.out.println(str.indexOf("l"));	// 返回的是第一个查找到的子字符串位置
		System.out.println(str.indexOf("l", 5));	// 从第6个元素开始查找子字符串位置
		System.out.println(str.lastIndexOf("l"));	// 从后开始查找指定字符串的位置
	}
}
程序执行结果:
5(“System.out.println(str.indexOf("world"));”语句执行结果)
2(“System.out.println(str.indexOf("l"));”语句执行结果)
8(“System.out.println(str.indexOf("l", 5));”语句执行结果)
8(“System.out.println(str.lastIndexOf("l"));”语句执行结果)

利用indexOf()方法判断子字符串是否存在
public class StringDemo {
	public static void main(String args[]) {
		String str = "helloworld“;	// 字符串对象
		if (str.indexOf("world") != -1) { // 能找到子字符串
			System.out.println("可以查询到数据。");
		}
	}
}
程序执行结果:
可以查询到数据。

使用contains()方法判断子字符串是否存在
public class StringDemo {
	public static void main(String args[]) {
		String str = "helloworld“;		// 字符串对象
		if (str.contains("world")) { 	// 子字符串存在
			System.out.println("可以查询到数据。");
		}
	}
}
程序执行结果:	可以查询到数据。
开头或结尾判断
public class StringDemo {
	public static void main(String args[]) {
		String str = "##@@hello**";		// 字符串对象
		System.out.println(str.startsWith("##"));// 是否以“##”开头
		System.out.println(str.startsWith("@@", 2));	// 从第2个索引开始是否以“@@”开头
		System.out.println(str.endsWith("**"));// 是否以“**”结尾
	}
}
程序执行结果:
true(“str.startsWith("##")”语句执行结果)
true(“str.startsWith("@@", 2)”语句执行结果)
true(“str.endsWith("**")”语句执行结果)

范例:观察替换的结果
public class StringDemo {
	public static void main(String args[]) {
		String str = "helloworld" ;			// 定义字符串
		String resultA = str.replaceAll("l","_") ;	// 全部替换
		String resultB = str.replaceFirst("l","_") ;	// 替换首个
		System.out.println(resultA) ;
		System.out.println(resultB) ;
	}
}
程序执行结果:
he__owor_d(“System.out.println(resultA)”语句执行结果)
he_loworld(“System.out.println(resultB)”语句执行结果)


验证字符串操作
public class StringDemo {
	public static void main(String args[]) {
		String str = "helloworld"; 		// 定义字符串
		String resultA = str.substring(5); 	// 从指定索引截取到结尾
		String resultB = str.substring(0, 5);	// 截取部分子字符串
		System.out.println(resultA);
		System.out.println(resultB);
	}
}
程序执行结果:
world(“System.out.println(resultA)”语句执行结果)
hello(“System.out.println(resultB)”语句执行结果)

进行全部拆分
public class StringDemo {
	public static void main(String args[]) {
		String str = "hello yootk nihao mldn";// 定义字符串,中间使用空格作为间隔
		String result[] = str.split(" ");	// 字符串拆分
		for (int x = 0; x < result.length; x++) {	// 循环输出
			System.out.print(result[x] + "、");
		}
	}
}

程序执行结果:
hello、yootk、nihao、mldn、

拆分为指定的个数
public class StringDemo {
	public static void main(String args[]) {
		String str = "hello yootk nihao mldn";	// 定义字符串,中间使用空格作为间隔
		String result[] = str.split(" ",2);		// 字符串拆分
		for (int x = 0; x < result.length; x++) {	// 循环输出
			System.out.println(result[x]);
		}
	}
}
程序执行结果:
hello
yootk nihao mldn

复杂拆分
public class StringDemo {
	public static void main(String args[]) {
		String str = "张三:20|李四:21|王五:22“;// 定义字符串
		String result[] = str.split("\|");	// 第一次拆分
		for (int x = 0; x < result.length; x++) {
			String temp[] = result[x].split(":");	// 第二次拆分
			System.out.println("姓名:" + temp[0] + ",年龄:" + temp[1]);
		}
	}
}
程序执行结果:
姓名:张三,年龄:20
姓名:李四,年龄:21
姓名:王五,年龄:22


字符串连接
public class StringDemo {
	public static void main(String args[]) {
		String str = "hello".concat("world") ;	// 等价于“+”
		System.out.println(str) ;
	}
}
程序执行结果:
helloworld

范例:转小写与大写操作
public class StringDemo {
	public static void main(String args[]) {
		String str = "(*(*Hello(*(*" ;		// 定义字符串
		System.out.println(str.toUpperCase()) ;	// 转大写后输出
		System.out.println(str.toLowerCase()) ;	// 转小写后输出
	}
}
程序执行结果:
(*(*HELLO(*(*(“System.out.println(str.toUpperCase())”语句执行结果)
(*(*hello(*(*(“System.out.println(str.toLowerCase())”语句执行结果)

去掉左右空格
public class StringDemo {
	public static void main(String args[]) {
		String str = "   hello   world  ";	// 定义字符串,包含空格
		System.out.println("【" + str + "】");// 原始字符串
		System.out.println("【" + str.trim() + "】");// 去掉空格后的字符串,中间的空格保留
	}
}
程序执行结果:
【   hello   world  】
【hello   world】

取得字符串长度
public class StringDemo {
	public static void main(String args[]) {
		String str = "helloworld";	// 定义字符串
		System.out.println(str.length());// 取得字符串长度
	}
}
程序执行结果:
10

范例:判断是否为空字符串
public class StringDemo {
	public static void main(String args[]) {
		String str = "helloworld";		// 定义字符串
		System.out.println(str.isEmpty()); 	// 判断字符串对象的内容是否为空字符串(不是null)
		System.out.println("".isEmpty());	 // 判断字符串常量的内容是否为空字符串(不是null)
	}
}
程序执行结果:
false(“System.out.println(str.isEmpty())”语句执行结果)
true(“System.out.println("".isEmpty())”语句执行结果)
实现首字母大写的操作
public class StringDemo {
	public static void main(String args[]) {
		String str = "yootk";	// 定义字符串
		System.out.println(initcap(str));// 调用initcap()方法
	}
	/**
	 * 实现首字母大写的操作
	 * @param temp 要转换的字符串数据
	 * @return 将首字母大写后返回
	 */
	public static String initcap(String temp) {
		// 利用substring(0,1)取出字符串的第一位而后将其大写,再连接剩余的字符串
		return temp.substring(0, 1).toUpperCase() + temp.substring(1);
	}
}
程序执行结果:
Yootk

原文地址:https://www.cnblogs.com/xuwei1/p/8353477.html