- length():返回字符串的长度。
String str = "hello world"; int len = str.length(); // len = 11
- charAt(index):返回指定索引位置的字符。
char ch = str.charAt(1); // ch = 'e'
- substring(beginIndex, endIndex):返回一个子字符串,从beginIndex开始,到endIndex结束(不包括endIndex)。
String subStr = str.substring(0, 5); // subStr = "hello"
- equals(obj):比较字符串是否相等,返回布尔值。
String str1 = "hello"; String str2 = "world"; boolean result1 = str1.equals(str2); // result1 = false String str3 = "hello"; String str4 = "hello"; boolean result2 = str3.equals(str4); // result2 = true
- equalsIgnoreCase(str):比较字符串是否相等,忽略大小写,返回布尔值。
String str1 = "hello"; String str2 = "HELLO"; boolean result1 = str1.equalsIgnoreCase(str2); // result1 = true String str3 = "world"; String str4 = "Hello"; boolean result2 = str3.equalsIgnoreCase(str4); // result2 = false
- indexOf(str):返回字符串中第一次出现指定子字符串的索引位置,如果未找到返回-1。
String str = "hello world"; int index1 = str.indexOf("o"); // index1 = 4 int index2 = str.indexOf("llo"); // index2 = 2 int index3 = str.indexOf("x"); // index3 = -1
- replace(oldChar, newChar):返回一个新的字符串,将所有旧字符替换为新字符。
String str = "hello world"; String newStr = str.replace("o", "0"); // newStr = "hell0 w0rld"
- split(regex):将字符串拆分为一个字符串数组,使用正则表达式分隔。
String str = "hello,world,java"; String[] arr = str.split(","); // arr = {"hello", "world", "java"}
- toUpperCase():将字符串转换为大写。
String str = "hello world"; String newStr = str.toUpperCase(); // newStr = "HELLO WORLD"
- toLowerCase():将字符串转换为小写。
String str = "HELLO WORLD"; String newStr = str.toLowerCase(); // newStr = "hello world"
- startsWith(prefix):判断字符串是否以指定的前缀开始,返回布尔值。
String str = "hello world"; boolean result = str.startsWith("hello"); // result = true
- endsWith(suffix):判断字符串是否以指定的后缀结束,返回布尔值
String str = "hello world"; boolean result = str.endsWith("world"); // result = true
- contains(str):判断字符串是否包含指定的子字符串,返回布尔值。
String str = "hello world"; boolean result = str.contains("lo"); // result = true
- isEmpty():判断字符串是否为空字符串,返回布尔值。
String str1 = ""; String str2 = "hello"; boolean result1 = str1.isEmpty(); // result1 = true boolean result2 = str2.isEmpty(); // result2 = false
- trim():返回一个新的字符串,去除字符串前后的空格
String str = " hello world "; String newStr = str.trim(); // newStr = "hello world"
- compareTo(str):比较字符串与指定字符串的字典顺序,返回一个整数,如果字符串在字典中排在指定字符串之前,则返回一个负数,如果它在指定字符串之后,则返回一个正数,如果它与指定字符串相等,则返回0。
String str1 = "hello"; String str2 = "world"; int result1 = str1.compareTo(str2); // result1 < 0 int result2 = str2.compareTo(str1); // result2 > 0 int result3 = str1.compareTo("hello"); // result3 = 0
- valueOf(obj):将指定的对象转换为字符串表示形式,返回一个新的字符串。
int num = 123; String str = String.valueOf(num); // str = "123"
- format(format, args):返回一个格式化字符串,使用指定的格式字符串和参数。
String str = String.format("Hello %s, your age is %d", "John", 30); // str = "Hello John, your age is 30"
- matches(regex):判断字符串是否匹配指定的正则表达式,返回布尔值。
String str1 = "hello"; String str2 = "hello123"; boolean result1 = str1.matches("[a-z]+"); // result1 = true boolean result2 = str2.matches("[a-z]+"); // result2 = false
- join(delimiter, elements):将多个字符串连接成一个字符串,使用指定的分隔符分隔,返回一个新的字符串。
String str = String.join(",", "hello", "world", "java"); // str = "hello,world,java"
java string类常用方法
最新推荐文章于 2024-07-25 17:45:03 发布