Java中的String类使用方法有哪些

发布时间:2023-05-17 17:32:02 作者:iii
来源:亿速云 阅读:129

Java中的String类使用方法有哪些

在Java编程中,String类是最常用的类之一,用于表示和操作字符串。String类提供了丰富的方法来操作字符串,包括字符串的创建、比较、查找、替换、分割等。本文将详细介绍Java中String类的常用方法及其使用方法。

1. 字符串的创建

在Java中,字符串可以通过多种方式创建:

1.1 使用字符串字面量

String str1 = "Hello, World!";

1.2 使用new关键字

String str2 = new String("Hello, World!");

1.3 使用字符数组

char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str3 = new String(charArray);

2. 字符串的比较

2.1 使用equals()方法

equals()方法用于比较两个字符串的内容是否相同。

String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1.equals(str2); // true

2.2 使用equalsIgnoreCase()方法

equalsIgnoreCase()方法用于比较两个字符串的内容是否相同,忽略大小写。

String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2); // true

2.3 使用compareTo()方法

compareTo()方法用于按字典顺序比较两个字符串。如果字符串相等,返回0;如果当前字符串在字典顺序上小于参数字符串,返回负数;否则返回正数。

String str1 = "apple";
String str2 = "banana";
int result = str1.compareTo(str2); // 负数

3. 字符串的查找

3.1 使用indexOf()方法

indexOf()方法用于查找指定字符或子字符串在字符串中首次出现的位置。如果未找到,返回-1。

String str = "Hello, World!";
int index = str.indexOf("World"); // 7

3.2 使用lastIndexOf()方法

lastIndexOf()方法用于查找指定字符或子字符串在字符串中最后一次出现的位置。如果未找到,返回-1。

String str = "Hello, World!";
int index = str.lastIndexOf("o"); // 8

3.3 使用contains()方法

contains()方法用于检查字符串是否包含指定的字符序列。

String str = "Hello, World!";
boolean contains = str.contains("World"); // true

4. 字符串的替换

4.1 使用replace()方法

replace()方法用于将字符串中的指定字符或子字符串替换为新的字符或子字符串。

String str = "Hello, World!";
String newStr = str.replace("World", "Java"); // "Hello, Java!"

4.2 使用replaceAll()方法

replaceAll()方法用于使用正则表达式替换字符串中的匹配项。

String str = "Hello, World!";
String newStr = str.replaceAll("o", "a"); // "Hella, Warld!"

4.3 使用replaceFirst()方法

replaceFirst()方法用于替换字符串中第一个匹配正则表达式的子字符串。

String str = "Hello, World!";
String newStr = str.replaceFirst("o", "a"); // "Hella, World!"

5. 字符串的分割

5.1 使用split()方法

split()方法用于根据正则表达式将字符串分割为子字符串数组。

String str = "apple,banana,orange";
String[] fruits = str.split(","); // ["apple", "banana", "orange"]

6. 字符串的截取

6.1 使用substring()方法

substring()方法用于从字符串中截取子字符串。

String str = "Hello, World!";
String subStr = str.substring(7); // "World!"
String subStr2 = str.substring(7, 12); // "World"

7. 字符串的大小写转换

7.1 使用toLowerCase()方法

toLowerCase()方法用于将字符串中的所有字符转换为小写。

String str = "Hello, World!";
String lowerStr = str.toLowerCase(); // "hello, world!"

7.2 使用toUpperCase()方法

toUpperCase()方法用于将字符串中的所有字符转换为大写。

String str = "Hello, World!";
String upperStr = str.toUpperCase(); // "HELLO, WORLD!"

8. 字符串的修剪

8.1 使用trim()方法

trim()方法用于去除字符串两端的空白字符。

String str = "  Hello, World!  ";
String trimmedStr = str.trim(); // "Hello, World!"

9. 字符串的长度

9.1 使用length()方法

length()方法用于获取字符串的长度。

String str = "Hello, World!";
int length = str.length(); // 13

10. 字符串的连接

10.1 使用concat()方法

concat()方法用于将指定字符串连接到此字符串的末尾。

String str1 = "Hello";
String str2 = "World";
String result = str1.concat(str2); // "HelloWorld"

10.2 使用+运算符

+运算符也可以用于连接字符串。

String str1 = "Hello";
String str2 = "World";
String result = str1 + str2; // "HelloWorld"

11. 字符串的格式化

11.1 使用format()方法

format()方法用于格式化字符串。

String str = String.format("Hello, %s!", "World"); // "Hello, World!"

12. 字符串的转换

12.1 使用toCharArray()方法

toCharArray()方法用于将字符串转换为字符数组。

String str = "Hello";
char[] charArray = str.toCharArray(); // ['H', 'e', 'l', 'l', 'o']

12.2 使用valueOf()方法

valueOf()方法用于将其他数据类型转换为字符串。

int num = 123;
String str = String.valueOf(num); // "123"

13. 字符串的匹配

13.1 使用matches()方法

matches()方法用于检查字符串是否匹配指定的正则表达式。

String str = "Hello, World!";
boolean matches = str.matches("Hello.*"); // true

14. 字符串的遍历

14.1 使用charAt()方法

charAt()方法用于获取字符串中指定位置的字符。

String str = "Hello";
char ch = str.charAt(1); // 'e'

15. 字符串的拼接

15.1 使用join()方法

join()方法用于使用指定的分隔符将多个字符串拼接成一个字符串。

String[] words = {"Hello", "World"};
String result = String.join(", ", words); // "Hello, World"

16. 字符串的重复

16.1 使用repeat()方法

repeat()方法用于将字符串重复指定次数。

String str = "Hello";
String repeatedStr = str.repeat(3); // "HelloHelloHello"

17. 字符串的空白检查

17.1 使用isBlank()方法

isBlank()方法用于检查字符串是否为空或仅包含空白字符。

String str = "   ";
boolean isBlank = str.isBlank(); // true

17.2 使用isEmpty()方法

isEmpty()方法用于检查字符串是否为空。

String str = "";
boolean isEmpty = str.isEmpty(); // true

18. 字符串的Unicode编码

18.1 使用codePointAt()方法

codePointAt()方法用于获取指定位置的字符的Unicode代码点。

String str = "Hello";
int codePoint = str.codePointAt(1); // 101

18.2 使用codePointBefore()方法

codePointBefore()方法用于获取指定位置前一个字符的Unicode代码点。

String str = "Hello";
int codePoint = str.codePointBefore(2); // 101

19. 字符串的Unicode字符数

19.1 使用codePointCount()方法

codePointCount()方法用于获取字符串中指定范围内的Unicode字符数。

String str = "Hello";
int count = str.codePointCount(0, 5); // 5

20. 字符串的Unicode字符遍历

20.1 使用codePoints()方法

codePoints()方法返回一个IntStream,包含字符串中所有字符的Unicode代码点。

String str = "Hello";
IntStream codePoints = str.codePoints();
codePoints.forEach(System.out::println);

21. 字符串的Unicode字符转换

21.1 使用toChars()方法

toChars()方法用于将指定字符的Unicode代码点转换为字符数组。

int codePoint = 65;
char[] chars = Character.toChars(codePoint); // ['A']

22. 字符串的Unicode字符检查

22.1 使用isLetter()方法

isLetter()方法用于检查指定字符是否为字母。

char ch = 'A';
boolean isLetter = Character.isLetter(ch); // true

22.2 使用isDigit()方法

isDigit()方法用于检查指定字符是否为数字。

char ch = '1';
boolean isDigit = Character.isDigit(ch); // true

22.3 使用isWhitespace()方法

isWhitespace()方法用于检查指定字符是否为空白字符。

char ch = ' ';
boolean isWhitespace = Character.isWhitespace(ch); // true

23. 字符串的Unicode字符转换

23.1 使用toLowerCase()方法

toLowerCase()方法用于将指定字符转换为小写。

char ch = 'A';
char lowerCh = Character.toLowerCase(ch); // 'a'

23.2 使用toUpperCase()方法

toUpperCase()方法用于将指定字符转换为大写。

char ch = 'a';
char upperCh = Character.toUpperCase(ch); // 'A'

24. 字符串的Unicode字符比较

24.1 使用compareTo()方法

compareTo()方法用于按Unicode值比较两个字符。

char ch1 = 'A';
char ch2 = 'B';
int result = Character.compare(ch1, ch2); // 负数

25. 字符串的Unicode字符转换

25.1 使用toString()方法

toString()方法用于将字符转换为字符串。

char ch = 'A';
String str = Character.toString(ch); // "A"

26. 字符串的Unicode字符转换

26.1 使用valueOf()方法

valueOf()方法用于将字符数组转换为字符串。

char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str = String.valueOf(charArray); // "Hello"

27. 字符串的Unicode字符转换

27.1 使用getBytes()方法

getBytes()方法用于将字符串转换为字节数组。

String str = "Hello";
byte[] bytes = str.getBytes(); // [72, 101, 108, 108, 111]

28. 字符串的Unicode字符转换

28.1 使用getChars()方法

getChars()方法用于将字符串中的字符复制到字符数组中。

String str = "Hello";
char[] charArray = new char[5];
str.getChars(0, 5, charArray, 0); // charArray = ['H', 'e', 'l', 'l', 'o']

29. 字符串的Unicode字符转换

29.1 使用toCharArray()方法

toCharArray()方法用于将字符串转换为字符数组。

String str = "Hello";
char[] charArray = str.toCharArray(); // ['H', 'e', 'l', 'l', 'o']

30. 字符串的Unicode字符转换

30.1 使用valueOf()方法

valueOf()方法用于将其他数据类型转换为字符串。

int num = 123;
String str = String.valueOf(num); // "123"

31. 字符串的Unicode字符转换

31.1 使用format()方法

format()方法用于格式化字符串。

String str = String.format("Hello, %s!", "World"); // "Hello, World!"

32. 字符串的Unicode字符转换

32.1 使用join()方法

join()方法用于使用指定的分隔符将多个字符串拼接成一个字符串。

String[] words = {"Hello", "World"};
String result = String.join(", ", words); // "Hello, World"

33. 字符串的Unicode字符转换

33.1 使用repeat()方法

repeat()方法用于将字符串重复指定次数。

String str = "Hello";
String repeatedStr = str.repeat(3); // "HelloHelloHello"

34. 字符串的Unicode字符转换

34.1 使用isBlank()方法

isBlank()方法用于检查字符串是否为空或仅包含空白字符。

String str = "   ";
boolean isBlank = str.isBlank(); // true

34.2 使用isEmpty()方法

isEmpty()方法用于检查字符串是否为空。

String str = "";
boolean isEmpty = str.isEmpty(); // true

35. 字符串的Unicode字符转换

35.1 使用codePointAt()方法

codePointAt()方法用于获取指定位置的字符的Unicode代码点。

String str = "Hello";
int codePoint = str.codePointAt(1); // 101

35.2 使用codePointBefore()方法

codePointBefore()方法用于获取指定位置前一个字符的Unicode代码点。

String str = "Hello";
int codePoint = str.codePointBefore(2); // 101

36. 字符串的Unicode字符转换

36.1 使用codePointCount()方法

codePointCount()方法用于获取字符串中指定范围内的Unicode字符数。

String str = "Hello";
int count = str.codePointCount(0, 5); // 5

37. 字符串的Unicode字符转换

37.1 使用codePoints()方法

codePoints()方法返回一个IntStream,包含字符串中所有字符的Unicode代码点。

String str = "Hello";
IntStream codePoints = str.codePoints();
codePoints.forEach(System.out::println);

38. 字符串的Unicode字符转换

38.1 使用toChars()方法

toChars()方法用于将指定字符的Unicode代码点转换为字符数组。

int codePoint = 65;
char[] chars = Character.toChars(codePoint); // ['A']

39. 字符串的Unicode字符转换

39.1 使用isLetter()方法

isLetter()方法用于检查指定字符是否为字母。

char ch = 'A';
boolean isLetter = Character.isLetter(ch); // true

39.2 使用isDigit()方法

isDigit()方法用于检查指定字符是否为数字。

char ch = '1';
boolean isDigit = Character.isDigit(ch); // true

39.3 使用isWhitespace()方法

isWhitespace()方法用于检查指定字符是否为空白字符。

char ch = ' ';
boolean isWhitespace = Character.isWhitespace(ch); // true

40. 字符串的Unicode字符转换

40.1 使用toLowerCase()方法

toLowerCase()方法用于将指定字符转换为小写。

char ch = 'A';
char lowerCh = Character.toLowerCase(ch); // 'a'

40.2 使用toUpperCase()方法

toUpperCase()方法用于将指定字符转换为大写。

char ch = 'a';
char upperCh = Character.toUpperCase(ch); // 'A'

41. 字符串的Unicode字符转换

41.1 使用compareTo()方法

compareTo()方法用于按Unicode值比较两个字符。

char ch1 = 'A';
char ch2 = 'B';
int result = Character.compare(ch1, ch2); // 负数

42. 字符串的Unicode字符转换

42.1 使用toString()方法

toString()方法用于将字符转换为字符串。

char ch = 'A';
String str = Character.toString(ch); // "A"

43. 字符串的Unicode字符转换

43.1 使用valueOf()方法

valueOf()方法用于将字符数组转换为字符串。

char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str = String.valueOf(charArray); // "Hello"

44. 字符串的Unicode字符转换

44.1 使用getBytes()方法

getBytes()方法用于将字符串转换为字节数组。

String str = "Hello";
byte[] bytes = str.getBytes(); // [72, 101, 108, 108, 111]

45. 字符串的Unicode字符转换

45.1 使用getChars()方法

getChars()方法用于将字符串中的字符复制到

推荐阅读:
  1. java中Arrays类的使用方法
  2. java中BufferedReader类的使用方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java string

上一篇:如何使用Go Java算法实现猜数字游戏

下一篇:springboot怎么整合并使用mybatis

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》