您好,登录后才能下订单哦!
在Java编程中,String
类是最常用的类之一,用于表示和操作字符串。String
类提供了丰富的方法来操作字符串,包括字符串的创建、比较、查找、替换、分割等。本文将详细介绍Java中String
类的常用方法及其使用方法。
在Java中,字符串可以通过多种方式创建:
String str1 = "Hello, World!";
new
关键字String str2 = new String("Hello, World!");
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str3 = new String(charArray);
equals()
方法equals()
方法用于比较两个字符串的内容是否相同。
String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1.equals(str2); // true
equalsIgnoreCase()
方法equalsIgnoreCase()
方法用于比较两个字符串的内容是否相同,忽略大小写。
String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2); // true
compareTo()
方法compareTo()
方法用于按字典顺序比较两个字符串。如果字符串相等,返回0;如果当前字符串在字典顺序上小于参数字符串,返回负数;否则返回正数。
String str1 = "apple";
String str2 = "banana";
int result = str1.compareTo(str2); // 负数
indexOf()
方法indexOf()
方法用于查找指定字符或子字符串在字符串中首次出现的位置。如果未找到,返回-1。
String str = "Hello, World!";
int index = str.indexOf("World"); // 7
lastIndexOf()
方法lastIndexOf()
方法用于查找指定字符或子字符串在字符串中最后一次出现的位置。如果未找到,返回-1。
String str = "Hello, World!";
int index = str.lastIndexOf("o"); // 8
contains()
方法contains()
方法用于检查字符串是否包含指定的字符序列。
String str = "Hello, World!";
boolean contains = str.contains("World"); // true
replace()
方法replace()
方法用于将字符串中的指定字符或子字符串替换为新的字符或子字符串。
String str = "Hello, World!";
String newStr = str.replace("World", "Java"); // "Hello, Java!"
replaceAll()
方法replaceAll()
方法用于使用正则表达式替换字符串中的匹配项。
String str = "Hello, World!";
String newStr = str.replaceAll("o", "a"); // "Hella, Warld!"
replaceFirst()
方法replaceFirst()
方法用于替换字符串中第一个匹配正则表达式的子字符串。
String str = "Hello, World!";
String newStr = str.replaceFirst("o", "a"); // "Hella, World!"
split()
方法split()
方法用于根据正则表达式将字符串分割为子字符串数组。
String str = "apple,banana,orange";
String[] fruits = str.split(","); // ["apple", "banana", "orange"]
substring()
方法substring()
方法用于从字符串中截取子字符串。
String str = "Hello, World!";
String subStr = str.substring(7); // "World!"
String subStr2 = str.substring(7, 12); // "World"
toLowerCase()
方法toLowerCase()
方法用于将字符串中的所有字符转换为小写。
String str = "Hello, World!";
String lowerStr = str.toLowerCase(); // "hello, world!"
toUpperCase()
方法toUpperCase()
方法用于将字符串中的所有字符转换为大写。
String str = "Hello, World!";
String upperStr = str.toUpperCase(); // "HELLO, WORLD!"
trim()
方法trim()
方法用于去除字符串两端的空白字符。
String str = " Hello, World! ";
String trimmedStr = str.trim(); // "Hello, World!"
length()
方法length()
方法用于获取字符串的长度。
String str = "Hello, World!";
int length = str.length(); // 13
concat()
方法concat()
方法用于将指定字符串连接到此字符串的末尾。
String str1 = "Hello";
String str2 = "World";
String result = str1.concat(str2); // "HelloWorld"
+
运算符+
运算符也可以用于连接字符串。
String str1 = "Hello";
String str2 = "World";
String result = str1 + str2; // "HelloWorld"
format()
方法format()
方法用于格式化字符串。
String str = String.format("Hello, %s!", "World"); // "Hello, World!"
toCharArray()
方法toCharArray()
方法用于将字符串转换为字符数组。
String str = "Hello";
char[] charArray = str.toCharArray(); // ['H', 'e', 'l', 'l', 'o']
valueOf()
方法valueOf()
方法用于将其他数据类型转换为字符串。
int num = 123;
String str = String.valueOf(num); // "123"
matches()
方法matches()
方法用于检查字符串是否匹配指定的正则表达式。
String str = "Hello, World!";
boolean matches = str.matches("Hello.*"); // true
charAt()
方法charAt()
方法用于获取字符串中指定位置的字符。
String str = "Hello";
char ch = str.charAt(1); // 'e'
join()
方法join()
方法用于使用指定的分隔符将多个字符串拼接成一个字符串。
String[] words = {"Hello", "World"};
String result = String.join(", ", words); // "Hello, World"
repeat()
方法repeat()
方法用于将字符串重复指定次数。
String str = "Hello";
String repeatedStr = str.repeat(3); // "HelloHelloHello"
isBlank()
方法isBlank()
方法用于检查字符串是否为空或仅包含空白字符。
String str = " ";
boolean isBlank = str.isBlank(); // true
isEmpty()
方法isEmpty()
方法用于检查字符串是否为空。
String str = "";
boolean isEmpty = str.isEmpty(); // true
codePointAt()
方法codePointAt()
方法用于获取指定位置的字符的Unicode代码点。
String str = "Hello";
int codePoint = str.codePointAt(1); // 101
codePointBefore()
方法codePointBefore()
方法用于获取指定位置前一个字符的Unicode代码点。
String str = "Hello";
int codePoint = str.codePointBefore(2); // 101
codePointCount()
方法codePointCount()
方法用于获取字符串中指定范围内的Unicode字符数。
String str = "Hello";
int count = str.codePointCount(0, 5); // 5
codePoints()
方法codePoints()
方法返回一个IntStream,包含字符串中所有字符的Unicode代码点。
String str = "Hello";
IntStream codePoints = str.codePoints();
codePoints.forEach(System.out::println);
toChars()
方法toChars()
方法用于将指定字符的Unicode代码点转换为字符数组。
int codePoint = 65;
char[] chars = Character.toChars(codePoint); // ['A']
isLetter()
方法isLetter()
方法用于检查指定字符是否为字母。
char ch = 'A';
boolean isLetter = Character.isLetter(ch); // true
isDigit()
方法isDigit()
方法用于检查指定字符是否为数字。
char ch = '1';
boolean isDigit = Character.isDigit(ch); // true
isWhitespace()
方法isWhitespace()
方法用于检查指定字符是否为空白字符。
char ch = ' ';
boolean isWhitespace = Character.isWhitespace(ch); // true
toLowerCase()
方法toLowerCase()
方法用于将指定字符转换为小写。
char ch = 'A';
char lowerCh = Character.toLowerCase(ch); // 'a'
toUpperCase()
方法toUpperCase()
方法用于将指定字符转换为大写。
char ch = 'a';
char upperCh = Character.toUpperCase(ch); // 'A'
compareTo()
方法compareTo()
方法用于按Unicode值比较两个字符。
char ch1 = 'A';
char ch2 = 'B';
int result = Character.compare(ch1, ch2); // 负数
toString()
方法toString()
方法用于将字符转换为字符串。
char ch = 'A';
String str = Character.toString(ch); // "A"
valueOf()
方法valueOf()
方法用于将字符数组转换为字符串。
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str = String.valueOf(charArray); // "Hello"
getBytes()
方法getBytes()
方法用于将字符串转换为字节数组。
String str = "Hello";
byte[] bytes = str.getBytes(); // [72, 101, 108, 108, 111]
getChars()
方法getChars()
方法用于将字符串中的字符复制到字符数组中。
String str = "Hello";
char[] charArray = new char[5];
str.getChars(0, 5, charArray, 0); // charArray = ['H', 'e', 'l', 'l', 'o']
toCharArray()
方法toCharArray()
方法用于将字符串转换为字符数组。
String str = "Hello";
char[] charArray = str.toCharArray(); // ['H', 'e', 'l', 'l', 'o']
valueOf()
方法valueOf()
方法用于将其他数据类型转换为字符串。
int num = 123;
String str = String.valueOf(num); // "123"
format()
方法format()
方法用于格式化字符串。
String str = String.format("Hello, %s!", "World"); // "Hello, World!"
join()
方法join()
方法用于使用指定的分隔符将多个字符串拼接成一个字符串。
String[] words = {"Hello", "World"};
String result = String.join(", ", words); // "Hello, World"
repeat()
方法repeat()
方法用于将字符串重复指定次数。
String str = "Hello";
String repeatedStr = str.repeat(3); // "HelloHelloHello"
isBlank()
方法isBlank()
方法用于检查字符串是否为空或仅包含空白字符。
String str = " ";
boolean isBlank = str.isBlank(); // true
isEmpty()
方法isEmpty()
方法用于检查字符串是否为空。
String str = "";
boolean isEmpty = str.isEmpty(); // true
codePointAt()
方法codePointAt()
方法用于获取指定位置的字符的Unicode代码点。
String str = "Hello";
int codePoint = str.codePointAt(1); // 101
codePointBefore()
方法codePointBefore()
方法用于获取指定位置前一个字符的Unicode代码点。
String str = "Hello";
int codePoint = str.codePointBefore(2); // 101
codePointCount()
方法codePointCount()
方法用于获取字符串中指定范围内的Unicode字符数。
String str = "Hello";
int count = str.codePointCount(0, 5); // 5
codePoints()
方法codePoints()
方法返回一个IntStream,包含字符串中所有字符的Unicode代码点。
String str = "Hello";
IntStream codePoints = str.codePoints();
codePoints.forEach(System.out::println);
toChars()
方法toChars()
方法用于将指定字符的Unicode代码点转换为字符数组。
int codePoint = 65;
char[] chars = Character.toChars(codePoint); // ['A']
isLetter()
方法isLetter()
方法用于检查指定字符是否为字母。
char ch = 'A';
boolean isLetter = Character.isLetter(ch); // true
isDigit()
方法isDigit()
方法用于检查指定字符是否为数字。
char ch = '1';
boolean isDigit = Character.isDigit(ch); // true
isWhitespace()
方法isWhitespace()
方法用于检查指定字符是否为空白字符。
char ch = ' ';
boolean isWhitespace = Character.isWhitespace(ch); // true
toLowerCase()
方法toLowerCase()
方法用于将指定字符转换为小写。
char ch = 'A';
char lowerCh = Character.toLowerCase(ch); // 'a'
toUpperCase()
方法toUpperCase()
方法用于将指定字符转换为大写。
char ch = 'a';
char upperCh = Character.toUpperCase(ch); // 'A'
compareTo()
方法compareTo()
方法用于按Unicode值比较两个字符。
char ch1 = 'A';
char ch2 = 'B';
int result = Character.compare(ch1, ch2); // 负数
toString()
方法toString()
方法用于将字符转换为字符串。
char ch = 'A';
String str = Character.toString(ch); // "A"
valueOf()
方法valueOf()
方法用于将字符数组转换为字符串。
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str = String.valueOf(charArray); // "Hello"
getBytes()
方法getBytes()
方法用于将字符串转换为字节数组。
String str = "Hello";
byte[] bytes = str.getBytes(); // [72, 101, 108, 108, 111]
getChars()
方法getChars()
方法用于将字符串中的字符复制到
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。