Java中常用类String的构造器与常用方法
在Java编程中,String
类是一个核心类,用于处理文本数据。掌握String
类的构造器和常用方法对于编写涉及文本操作的程序至关重要。本文将为初学者介绍String
类的一些常用构造器和常用方法,并通过示例代码进行说明。
1. 构造器(Constructors)
String
类提供了多个构造器,用于创建String
对象。以下是一些常用的构造器:
1.1 通过字符串字面值创建
// 直接使用字符串字面值创建String对象
String str1 = "Hello, World!";
这里并没有直接使用构造器,但是字符串字面值实际上是通过一个特殊的构造器来创建String
对象的。这个构造器是String
类的一个私有构造器,用于字符串常量池中的字符串。
1.2 通过字符数组创建
// 使用字符数组创建String对象
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str2 = new String(charArray);
// 或者指定一个偏移量和长度
String str3 = new String(charArray, 1, 3); // 结果为 "ell"
1.3 通过字节数组创建
// 使用字节数组创建String对象(注意,这种方式会假定字节数组是UTF-8编码的)
byte[] byteArray = {72, 101, 108, 108, 111}; // 对应 "Hello" 的UTF-8编码
String str4 = new String(byteArray);// 也可以指定字符集
String str5 = new String(byteArray, StandardCharsets.UTF_8);
1.4 通过另一个String对象创建
// 使用另一个String对象创建新的String对象(实际上是复制)
String str6 = new String("Hello, World!");
2. 常用方法(Methods)
String
类提供了许多方法来操作字符串。以下是一些常用的方法:
2.1 获取字符串长度
String str = "Hello, World!";
int length = str.length(); // 返回13
2.2 获取指定位置的字符
char charAt = str.charAt(0); // 返回 'H'
2.3 字符串连接
String str1 = "Hello";
String str2 = "World";
String concatenated = str1 + " " + str2; // 返回 "Hello World"
或者使用concat
方法:
String concatenated = str1.concat(" ").concat(str2); // 返回 "Hello World"
2.4 比较字符串
// 使用equals方法比较字符串内容是否相等
boolean isEqual = str.equals("Hello, World!"); // 返回true// 使用equalsIgnoreCase方法忽略大小写比较字符串内容是否相等
boolean isEqualIgnoreCase = str.equalsIgnoreCase("hello, world!"); // 返回true// 使用compareTo方法比较字符串字典顺序(基于Unicode码点)
int compareResult = str.compareTo("Hello, Java!"); // 返回一个负数,因为"World"在"Java"之后
2.5 查找子字符串
// 使用indexOf方法查找子字符串首次出现的位置
int index = str.indexOf("World"); // 返回7// 查找子字符串从指定位置开始首次出现的位置
int fromIndexIndex = str.indexOf("o", 8); // 返回10,因为从索引8开始查找
2.6 截取子字符串
// 使用substring方法截取子字符串
String subStr = str.substring(7); // 从索引7开始到字符串末尾,返回 "World!"
String subStrWithLength = str.substring(0, 5); // 从索引0开始,长度为5的子字符串,返回 "Hello"
2.7 替换字符串中的子串
// 使用replace方法替换字符串中的子串
String replaced = str.replace("World", "Java"); // 返回 "Hello, Java!"
2.8 转换为大写或小写
// 转换为大写
String upperCase = str.toUpperCase(); // 返回 "HELLO, WORLD!"// 转换为小写
String lowerCase = str.toLowerCase(); // 返回 "hello, world!"
2.9 去除字符串两端的空格
// 使用trim方法去除字符串两端的空格
String trimmed = " Hello, World! ".trim(); // 返回 "Hello, World!"
2.10 判断字符串是否为空或null
// 使用isEmpty方法判断字符串是否为空(即长度为0)
boolean isEmpty = "".isEmpty(); // 返回true// 使用null判断字符串是否为null
String nullStr = null;
boolean isNull = nullStr == null; // 返回true// 同时判断字符串是否为null或空
String mayBeNullOrEmpty = "";
boolean isNullOrEmpty = mayBeNullOrEmpty == null || mayBeNullOrEmpty.isEmpty(); // 对于空字符串返回true
2.11 分割字符串
// 使用split方法根据指定分隔符分割字符串
String[] words = "Hello,World,Java".split(","); // 返回 ["Hello", "World", "Java"]
2.12 字符串的格式化
// 使用String.format方法进行字符串格式化
String formatted = String.format("Hello, %s! Today is %s.", "World", "Monday"); // 返回 "Hello, World! Today is Monday."
示例代码
下面是一个综合了上述常用方法的简单示例代码:
public class StringDemo {public static void main(String[] args) {String str = " Hello, World! ";// 获取长度int length = str.length();System.out.println("Length: " + length); // 输出 "Length: 14"// 去除两端空格String trimmed = str.trim();System.out.println("Trimmed: " + trimmed); // 输出 "Hello, World!"// 查找子字符串位置int index = trimmed.indexOf("World");System.out.println("Index of 'World': " + index); // 输出 "Index of 'World': 7"// 截取子字符串String subStr = trimmed.substring(7);System.out.println("Substring: " + subStr); // 输出 "World!"// 替换字符串中的子串String replaced = trimmed.replace("World", "Java");System.out.println("Replaced: " + replaced); // 输出 "Hello, Java!"// 转换为大写String upperCase = replaced.toUpperCase();System.out.println("Upper case: " + upperCase); // 输出 "HELLO, JAVA!"// 分割字符串String[] words = replaced.split(",");for (String word : words) {System.out.println(word); // 分别输出 "Hello" 和 " Java!"}}
}
总结
String
类是Java中非常重要的一个类,它提供了丰富的方法来操作字符串。本文介绍了String
类的一些常用构造器和常用方法,并通过示例代码进行了说明。初学者应该熟练掌握这些基本操作,以便在编写涉及文本处理的程序时能够灵活运用。