常用方法
1.判断功能的方法
- public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
- public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小
写。
演示代码:
package StringTest;
/**
* @Author: mcc
* @Date: 2021/8/8
* @Description: StringTest
* @version: 1.0
*/
public class StringDemo1 {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = "Abc";
boolean flag1 = s1.equals(s2);
System.out.println(flag1);
System.out.println("==============");
boolean flag2 = s1.equals(s3);
System.out.println(flag2);
System.out.println("==============");
boolean flag3 = s1.equalsIgnoreCase(s3);
System.out.println(flag3);
}
}
测试截图:
2.获取功能的方法
- public int length () :返回此字符串的长度。
- public String concat (String str) :将指定的字符串连接到该字符串的末尾。
- public char charAt (int index) :返回指定索引处的 char值。
- public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
- public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符
串结尾。- public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
代码演示
package StringTest;
/**
* @Author: mcc
* @Date: 2021/8/8
* @Description: StringTest
* @version: 1.0
*/
public class StringDemo2 {
public static void main(String[] args) {
String str = "liuhaocun";
String concatStr = "beautiful";
// public int length () :返回此字符串的长度。
int length = str.length();
System.out.println(length);
System.out.println("==============");
// public String concat (String str) :将指定的字符串连接到该字符串的末尾。
String concat = str.concat(concatStr);
System.out.println(concat);
System.out.println(str + concatStr);//一般正常使用这种方式
System.out.println("==============");
// public char charAt (int index) :返回指定索引处的 char值。一般用于遍历
char ch = str.charAt(0);
System.out.println(ch);
char c = str.charAt(1);
System.out.println(c);
System.out.println("==============");
// public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
int liu = str.indexOf("liu");
int i = str.indexOf(c);
System.out.println(liu);
System.out.println(i);
System.out.println("==============");
// public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符
// 串结尾。
String substring = str.substring(3);
System.out.println(substring);
System.out.println("==============");
// public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
String substring1 = str.substring(0, 3);
System.out.println(substring1);
System.out.println("==============");
}
}
测试截图:
3.转换功能的方法
- public char[] toCharArray () :将此字符串转换为新的字符数组。
- public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
- public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
代码演示
package StringTest;
/**
* @Author: mcc
* @Date: 2021/8/8
* @Description: StringTest
* @version: 1.0
*/
public class StringDemo3 {
public static void main(String[] args) {
String str ="liuhaocunbeautiful";
// public char[] toCharArray () :将此字符串转换为新的字符数组。
char[] c = str.toCharArray();
for (char c1 : c) {
System.out.print(c1+" ");
}
System.out.println();
System.out.println("==========================================");
// public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
byte[] bytes = str.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.print(bytes[i]+" ");
}
System.out.println();
System.out.println("=================================");
// public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
String replace = str.replace("liuhaocun", "zhaowei");
System.out.println(replace);
}
}
测试截图:
4.分割功能的方法
- public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
代码演示
package StringTest;
/**
* @Author: mcc
* @Date: 2021/8/8
* @Description: StringTest
* @version: 1.0
*/
public class StringDemo4 {
public static void main(String[] args) {
String s = "aa,bb,cc";
String[] strArray = s.split(","); // ["aa","bb","cc"]
for (String s1 : strArray) {
System.out.print(s1);
}
}}
测试截图