- //演示创建、获取操作
- public class StringDemo {
- public static void main(String[] args) {
- //创建字符串
- String s1 = new String("ABCDEF");
- String s2 = "EFGHI";
-
- //获取字符串的长度
- int length = s1.length();
- System.out.println(length);
-
- //根据index获取指定位置的char
- char c = s1.charAt(0);
- System.out.println(c);
-
- //返回'C'在字符串中第一次出现的位置,没有找到返回-1
- int p0 = s1.indexOf('C');
- int p1 = s1.indexOf('J');
- System.out.println(p0);
- System.out.println(p1);
- System.out.println();
-
- //从指定位置开始,获取'C'在字符串中第一次出现的位置
- int p2 = s1.indexOf('C', 2);
- int p3 = s1.indexOf('C', 3);
- System.out.println(p2);
- System.out.println(p3);
- System.out.println();
-
- //返回"CDE"在字符串中第一次出现的位置
- int p4 = s1.indexOf("CDE");
- System.out.println(p4);
-
- //从指定位置开始,获取"CDE"在字符串中第一次出现的位置
- int p5 = s1.indexOf("CDE", 1);
- System.out.println(p5);
-
- //从后向前搜索返回第一次'C'出现的位置
- //注意返回值仍然是2。反向的是搜索方向,而不是索引
- int p6 = s1.lastIndexOf('C');
- System.out.println(p6);
-
- String s3 = "ABCABCABC";
- int p7 = s3.lastIndexOf('C');
- System.out.println(p7);
- }
- }
复制代码- //演示判断操作
- public class StringDemo2 {
- public static void main(String[] args) {
- //先创建个字符串
- String s1 = "This is a String type.";
-
- //字符串中是否包含子串" is a"
- boolean b1 = s1.contains(" is a");
- System.out.println(b1);
-
- //字符串是否为空
- boolean b2 = s1.isEmpty();
- System.out.println(b2);
-
- //字符串是否以"Thi"开头
- boolean b3 = s1.startsWith("Thi");
- System.out.println(b3);
-
- //字符串是否以"type."结尾
- Boolean b4 = s1.endsWith("type.");
- System.out.println(b4);
-
- //比较Unicode序列是否相同
- String s2 = new String("This is a String type.");
- Boolean b5 = s1.equals(s2);
- System.out.println(b5);
-
- //比较Unicode序列是否相同,忽略大小写
- String s3 = new String("ThiS is A string tyPe.");
- Boolean b6 = s1.equalsIgnoreCase(s3);
- System.out.println(b6);
- }
- }
复制代码- //演示转换操作
- public class StringDemo3 {
- public static void main(String[] args) {
- //将字符串转换为char[]
- char[] chars = "This is a String type.".toCharArray();
-
- //构造函数,将char[]转换为字符串
- String s1 = new String(chars);
- String s2 = new String(chars, 2, 5);
- System.out.println(s1);
- System.out.println(s2);
- System.out.println();
-
- //静态方法,将char[]转换为字符串
- String s3 = String.copyValueOf(chars);
- String s4 = String.copyValueOf(chars, 3, 4);
- String s5 = String.valueOf(chars);
- System.out.println(s3);
- System.out.println(s4);
- System.out.println(s5);
- System.out.println();
-
- //强字符串转换为byte[]
- byte[] bytes = "This is a String type.".getBytes();
- System.out.println(bytes);
-
- //构造函数,将char[]转换为字符串
- String s6 = new String(bytes);
- String s7 = new String(bytes, 3, 4);
- System.out.println(s6);
- System.out.println(s7);
- System.out.println();
-
- //将基本数据类型转换为字符串
- String s8 = String.valueOf(1.27);
- String s9 = String.valueOf(3.14f);
- System.out.println(s8);
- System.out.println(s9);
- }
- }
复制代码
- import java.util.Arrays;
- public class StringDemo4 {
- public static void main(String[] args) {
- //先创建一个字符串
- String s1 = "abc def abc abcdefgh";
-
- //替换指定字符'c'
- String s2 = s1.replace('c', 'm');
- System.out.println(s2);
-
- //以" "切割字符串
- String[] arr = s1.split(" ");
- System.out.println(Arrays.toString(arr));
-
- //获取字符串从索引3开始的一部分
- String s3 = s1.substring(3);
- System.out.println(s3);
-
- //获取字符串从索引3开始5结尾(不包括5)的一部分
- String s4 = s1.substring(3, 5);
- System.out.println(s4);
-
- //将字符串转换为大写
- String s5 = s1.toUpperCase();
- System.out.println(s5);
-
- //将字符串转换为小写
- String s6 = s1.toLowerCase();
- System.out.println(s6);
-
- //将字符串两端的多个空格去除
- String s7 = " abc ";
- String s8 = s7.trim();
- System.out.println(s8);
-
- //将两个字符串进行自然顺序比较
- String s9 = "abc";
- int x = s1.compareTo(s9);
- System.out.println(x);
- }
- }
复制代码
|
|