Java String类详解

目录

1. 获取指定角标位置的字符

2. 字符串比较

3. 字符串连接另一个字符串

4. 判断字符串是否包含指定的子串

5. 判断字符串是否以某个子串为前缀、后缀

6. 判断两个字符串是否相等

7. 转换字节、字符数组

8. 获取指定子串在字符串中的角标位置

9. 获取字符串长度

10. 将字符串变大、小写

11. 从字符串中获取子串

12. 去除字符串中的首尾空格

14. 其它对象转换为字符串

15. 有关正则表达式的字符串操作


1. 获取指定角标位置的字符

API: char charAt(int index);

案例?:

String info = "I Love China !";
System.out.println(info.charAt(2));

2. 字符串比较

API: int compareTo(String anotherString);单纯比较,一个个的字符进行比较

API: int compareToIgnoreCase(String str);忽略大小写比较

案例?:

String info = "I Love China !";
String another = "i love China !";
System.out.println(info.compareTo(another));
System.out.println(info.compareToIgnoreCase(another));

3. 字符串连接另一个字符串

API: String concat(String str);

案例?:

String info = "I Love China !";
String another = "i love China !";
String str = info.concat(another);
System.out.println(str);

4. 判断字符串是否包含指定的子串

API: boolean contains(CharSequence cs);

案例?:

String info = "I Love China !";
String key = "China";
System.out.println(info.contains(key));

5. 判断字符串是否以某个子串为前缀、后缀

API: boolean startsWith(String pregex);前缀

API: boolean endsWith(String suffix);后缀

案例?:

String info = "I Love China !";
System.out.println(info.startsWith("I"));
System.out.println(info.endsWith("!"));

6. 判断两个字符串是否相等

API: boolean equals(Object anObject);不忽略大小写

API: boolean equals(String anotherString);忽略大小写

案例?:

String info = "I Love China !";
String another = "i love China !";
System.out.println(info.equals(another));
System.out.println(info.equalsIgnoreCase(another));

7. 转换字节、字符数组

API: byte[] getBytes(); 转换字节数组

API: char[] toCharArray();转换字符数组

案例?:

String info = "I Love China !";
char[] chars = info.toCharArray();
byte[] bytes = info.getBytes();

8. 获取指定子串在字符串中的角标位置

API: int indexOf(String str);从头开始,返回的角标位置是第一个字符的角标位置

API: int indexOf(String str, int fromIndex);从指定角标位置开始

API: int lastIndexOf(String str);从后开始,返回的角标位置是第一个字符的角标位置

API: int lastIndexOf(String str, int fromIndex);从指定位置开始向前查找(这是一个坑)

案例?:

String info = "I Love China !";
System.out.println(info.indexOf("Love"));
System.out.println(info.indexOf("China", 5));
System.out.println(info.lastIndexOf("Love"));
System.out.println(info.lastIndexOf("China", 12));

9. 获取字符串长度

API: int length();获取长度

API: boolean isEmpty();判断字符串是否为空(当字符串 length 为0是返回true)有点鸡肋

案例?:

String info = "I Love China !";
System.out.println(info.length());
System.out.println(info.isEmpty());

10. 将字符串变大、小写

API: String toUpperCase();变大写

API: String toLowerCase();变小写

案例?:

String info = "I Love China !";
System.out.println(info.toUpperCase());
System.out.println(info.toLowerCase());

11. 从字符串中获取子串

API: String substring(int beginIndex); 从指定角标到最后一个字符

API: String substring(int beginIndex, int endIndex);从开始角标到结束角标(包含头不包含尾)

案例?:

String info = "I Love China !";
System.out.println(info.substring(2));
System.out.println(info.substring(2, 7));

12. 去除字符串中的首尾空格

API: String trim();

案例?:

String info = "         I Love China !";
System.out.println(info.trim());

14. 其它对象转换为字符串

API: String valueOf(T);

案例?:

int a = 12;
String number = String.valueOf(a);
System.out.println(number);

注意点:对于自定义对象,调用 valueOf api时,自定义对象自身要实现 toString() 方法

15. 有关正则表达式的字符串操作

API: boolean matches(String regex);该字符串是否匹配传入的正则表达式

API: String[] split(String regex);根据传入的正则表达式,将字符串切分为字符串数组

API: String replaceFirst(String regex, String replacement);替换满足正则表达式的第一个子串

API: String replaceAll(String regex, String replacement);将字符串中匹配到的所有子串用传入的字串进行替换

API: String replace(char oldChar, char newChar);将字符串中的所有旧的字符替换成新的字符

API: String replace(CharSequence target, CharSequence replacement);将字符串中目标子串替换成新的子串

例如?:匹配含有a-z或0-9,1次到多次

String str = "adfs123456";
String regex = "[a-z0-9]+";
System.out.println(str.matches(regex));

例如?:以空格、逗号切割字符串,获得字符串数组

String str = "I Love,China!";
String regex = "[ ,]";
System.out.println(Arrays.toString(str.split(regex)));

例如?:将字符串中非字母(包括大写小写)去除

String str = "I Love,China!";
String regex = "[^a-zA-Z]";
System.out.println(str.replaceAll(regex, ""));

想了解更多的关于正则表达式有关的知识,请点击下面的链接!

https://blog.csdn.net/assiduous_me/article/details/89291444

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值