1、字符串的【构造】方法:
创建一个字符串。
String s = "abcd";
(1)无参构造创建对象。
String s = new String();
(2)把byte数组转换成字符串,通过数组作为参数,
byte[] arr = {1,2,3};
String s1 = new String(arr);
(3)把char数组转换成字符串,通过数组作为参数,
char[] arr1 = {'a','b','c','d'};
String s2 = new String(arr1);
(4)把char数组一部分转化成,字符串。
String(char[] value, int offset, int count)
char[] arr2 = {'a','b','c','d'};
String s3 = new String(arr2,1,2);
2、字符串的【判断】方法
(1)、startsWith(String prefix)
测试此字符串是否以指定的前缀开始。
(2)、endsWith(String suffix)
测试此字符串是否以指定的后缀结束。
(3)、 isEmpty()
当且仅当 length() 为 0 时返回 true。
(4)、equals(Object anObject)
将此字符串与指定的对象比较。相等为true
(5)、equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。
(6)、contains(CharSequence s)
当且仅当此字符串包含指定的 判断字符串是否包含某个子字符串。
char 值序列时,返回 true。
3、字符串的【获取】方法
截长取位取元素
(1)length()
返回此字符串的长度。
(2)、indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
(3)、lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引。
(4)、charAt(int index)
返回指定索引处的 char 值。
(5)、substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
代码练习:
public class Test04 {
public static void main(String[] args) {
//定义一个字符串,用来从测试他的【获取】方法
String s = "abcdefg_abcdefg_";
//(1)length() 返回此字符串的长度。
int len = s.length(); //返回字符串的长度。
System.out.println(len); //16
//(2)、indexOf(String str, int fromIndex)
//返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
System.out.println(s.indexOf("bcd",4)); // 9
//如果没有指定位置开始找,就从头找,输出第一次参数字符串出现的索引。
System.out.println(s.indexOf("bcd")); //1
//(3)、lastIndexOf(int ch)
//返回指定字符在此字符串中最后一次出现处的索引。
System.out.println(s.lastIndexOf("_")); //15 索引位置
//(4)、charAt(int index) 返回指定索引处的 char 值。
System.out.println(s.charAt(2)); //c
//(5)、substring(int beginIndex, int endIndex)
//返回一个新字符串,它是此字符串的一个子字符串。
System.out.println(s.substring(3,6)); //def
}
}
4、字符串的【转换】方法
转换:大小拼串转数组
(1)、toUpperCase()
将此 String 中的所有字符都转换为大写。
(2)、toLowerCase()
将此 String 中的所有字符都转换为小写。
(3)、concat(String str)
将指定字符串连接到此字符串的结尾。
(4)、toCharArray()
将此字符串转换为一个新的字符数组。
(5)代码练习:
public class Test05 {
public static void main(String[] args) {
String s = "abcdeABCDE";
//(1)、toUpperCase()
//将此 String 中的所有字符都转换为大写。
String upperStr= s.toUpperCase();
System.out.println(upperStr);
//(2)、toLowerCase() 将此 String 中的所有字符都转换为小写。
System.out.println(s.toLowerCase());
//(3)、concat(String str) 将指定字符串连接到此字符串的结尾。
System.out.println(s.concat("1111111111"));
//(4)、toCharArray() 将此字符串转换为一个新的字符数组。
char[] str_arr = s.toCharArray();
System.out.println(str_arr.toString());
//高级for循环。 fore alt+/ 可以快速生成。
for (char c : str_arr) {
System.out.println(c);
}
}
}
5、其他
(1)、trim() 把字符串前后的空格去掉。
(2)、replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar
(3)、compareTo(String anotherString)
按字典顺序比较两个字符串。
(4)valueOf(其他类型数据),把任意类型的数据转换为字符串。
代码练习:
public class Test06 {
public static void main(String[] args) {
// 字符串 ,其他的方法去空替换字典排
String s =" abcdd efg ";
//(1)、trim() 把字符串前后的空格去掉。
System.out.println(s.trim()); //"abcdd efg"
//(2)、replace(char oldChar, char newChar)
// 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar
System.out.println(s.replace("abc", "aaa")); //" aaadd efg "
//(3)、compareTo(String anotherString)
//按字典顺序比较两个字符串。
System.out.println("a".compareTo("d"));
//-1 如果按字典顺序此 String 对象位于参数字符串之前,则比较结果为一个负整数
//代表前面的字符串,排序靠前。
//。如果按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数
System.out.println("b".compareTo("a")); //1
//(4)valueOf(其他类型数据),把任意类型的数据转换为字符串。
String new_str1 = "".valueOf(true);
System.out.println(new_str1); //把布尔类型数据,转化成字符串:"true"
String new_str2 = "".valueOf(123131);
System.out.println(new_str2); //把int类型数据,转换成字符串。"123131"
}
}