String最常用方法

478 阅读1分钟

代码结构

继承的很少、大多都是自己的方法

image.png

image.png

image.png

大小写转换方法

public String toLowerCase(Locale locale)
public String toLowerCase()
public String toUpperCase(Locale locale)
public String toUpperCase()
String uos = "WIN64";
String los = "linux64";
Locale loc = Locale.forLanguageTag("tr-TR");
System.out.println(uos.toLowerCase(loc));
System.out.println(los.toUpperCase(loc));
System.out.println(uos.toLowerCase(Locale.ROOT));
System.out.println(los.toUpperCase(Locale.ROOT));

wın64
LİNUX64
win64
LINUX64
比较字符串相等、忽略大小写

1、直接比较
String str = "";
str.equalsIgnoreCase("b");
2、先整体统一大小写、再用equals比较
str.toUpperCase();
str.toLowerCase();
3、单个字符转大小写、再比较
Character.toUpperCase(str.charAt(0));

和字符相关方法

public char charAt(int index)
// 范围内则取值、超范围则越界
String str = "abc";
System.out.println(str.charAt(0));
System.out.println(str.charAt(2));
System.out.println(str.charAt(3));
a
c
String index out of range: 3
public char[] toCharArray()
String str = "abc";
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
    System.out.println(chars[i]);
}
a
b
c
// 判断字符相等方式
if (s.charAt(i) - c == 0)
if (s.charAt(i) == c)

截取

public String[] split(String regex)
public String[] split(String regex, int limit)

java split(String regex, int limit) 和 split(String regex)的区别

public String substring(int beginIndex)
// 从对应下标到最后一个元素
String str = "abcdefg";
System.out.println(str.substring(0)); // abcdefg
System.out.println(str.substring(2)); // cdefg
System.out.println(str.substring(6)); // g
System.out.println(str.substring(7)); // 空 下标没有7但是不报错

System.out.println(str.substring(8)); // java.lang.StringIndexOutOfBoundsException: String index out of range: -1

System.out.println(str.substring(-1)); // java.lang.StringIndexOutOfBoundsException: String index out of range: -1
public String substring(int beginIndex, int endIndex)
// Params: 第一位起始下标、第二位终止下标、但是左闭右开区间
// beginIndex – the beginning index, inclusive.
// endIndex – the ending index, exclusive.
String str = "abcdefg";
System.out.println(str.substring(0, 1));             // a
System.out.println(str.substring(0, 0));             // 空
System.out.println(str.substring(0, str.length()));  // abcdefg

System.out.println(str.substring(0, str.length() + 1)); // java.lang.StringIndexOutOfBoundsException: String index out of range: 8
System.out.println(str.substring(-1, str.length()));    // java.lang.StringIndexOutOfBoundsException: String index out of range: -1
// Params: 第一位起始下标、第二位终止下标、但是左闭右开区间
// beginIndex – the beginning index, inclusive.
// endIndex – the ending index, exclusive.
public CharSequence subSequence(int beginIndex, int endIndex)

// subSequence()方法用于返回字符序列的新集合,该字符序列是该序列的子集
String str = "abcdefg";
CharSequence charSequence = str.subSequence(0, 1);               // a
CharSequence charSequence = str.subSequence(0, 1);               // 空
CharSequence charSequence = str.subSequence(0, str.length());    // abcdefg


System.out.println(str.subSequence(0, str.length() + 1)); // java.lang.StringIndexOutOfBoundsException: String index out of range: 8

System.out.println(str.subSequence(-1, str.length()));    // java.lang.StringIndexOutOfBoundsException: String index out of range: -1