目录
前言:
1.String类的概述
2.字符串的两种创建方式
1.String s = “abc”;
2.String s = new String(“abc”);
3.String类常用方法
前言:
String类是引用类型,不是基本类型。
1.String类的概述
String就是字符串,字符串是由多个字符组成的一串数据(字符序列)的字符串常量,java中所有字符串都是String类的实例.例如:"张三"、"abc"、"123",像这种被 " " 号括起来的都是String类的对象
2.字符串的两种创建方式
1.String s = “abc”;
1.String s = “abc”;
先在栈中创建一个对String类的对象引用变量s,然后去字符串常量池中查找有没有"abc", 如果没有则在常量池中添加”abc”, s引用变量指向常量池中 的”abc”,如果常量池中有,则直接指向改地址即可,不用重新创建.
2.String s = new String(“abc”);
一概在堆中创建新对象,值存储在堆内存的对象中。
public class StringDemo {
public static void main(String[] args) {
//String有两种赋值的办法
String a = "abc";
a+= "def";
a+="jhi";
System.out.println(a);
String s1 = "abc";
String s2 = "adc";
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s3==s4);//false
System.out.println(s3.equals(s4));//true
}
}
构造方法 :
public String()
public String(String str)
public String(byte[] bytes)
public String(char[] value)
3.String类常用方法
判断功能
boolean equals(Object obj) 判断字符串内容是否相等
boolean equalsIgnoreCase(String str) 判断两个对象中的内容是否相等 忽略大小写
boolean contains(String str) 判断是否包含指定字符串
boolean isEmpty() 判断是否为空("") 为空返回true
boolean startsWith(String prefix) 判断是否以指定的字符串开头
boolean endsWith(String suffix) 判断是否以指定的字符串结尾
int compareTo(String anotherString) 比较两个字符串的位置
public static void main(String[] args) {
String s = "abcdedfg";
// 01234567
System.out.println(s.length());
char c = s.charAt(3);//返回指定索引的字符
System.out.println(c);
int index = s.indexOf("d");//获取指定字符串首次出现的位置 从前向后找
System.out.println(index);
int index1 = s.indexOf("d",4);//从指定的位置开始,获取指定字符串首次出现的位置
System.out.println(index1);
int index2 = s.lastIndexOf("d");
System.out.println(index2);
String s3 = "abcdedfg";
// 01234567
String s4 = s3.substring(2); //从s3 截取指定区间的字符串 从指定位置开始 到 结束
System.out.println(s3);//abcdedfg
System.out.println(s4);//cdedfg
String s5 =s3.substring(2,6);//k指定区间截取 开始位置 -- 结束位置(不包含结束)
System.out.println(s5);
}
获取功能 :
int length() 返回字符串长度
char charAt(int index) 返回指定索引的字符
int indexOf(String str) 获取指定字符串首次出现的位置 从前向后找
int indexOf(String str,int fromIndex) 从指定的位置开始,获取指定字符串首次出现的位置
String substring(int start) 截取指定区间的字符串 从指定位置开始 到 结束
String substring(int start,int end) 指定区间截取 开始位置 – 结束位置(不包含结束)
public static void main(String[] args) {
String s = "abcdedfg";
// 01234567
System.out.println(s.length());
char c = s.charAt(3);//返回指定索引的字符
System.out.println(c);
int index = s.indexOf("d");//获取指定字符串首次出现的位置 从前向后找
System.out.println(index);
int index1 = s.indexOf("d",4);//从指定的位置开始,获取指定字符串首次出现的位置
System.out.println(index1);
int index2 = s.lastIndexOf("d");
System.out.println(index2);
String s3 = "abcdedfg";
// 01234567
String s4 = s3.substring(2); //从s3 截取指定区间的字符串 从指定位置开始 到 结束
System.out.println(s3);//abcdedfg
System.out.println(s4);//cdedfg
String s5 =s3.substring(2,6);//k指定区间截取 开始位置 -- 结束位置(不包含结束)
System.out.println(s5);
}
转换功能 :
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
String toLowerCase() 转小写
String toUpperCase() 转大写
String concat(String str) 两个字符串拼接 并返回新的字符串对象.
Stirng[] split(分割符); 用指定符号 来分割字符串为数组
public static void main(String[] args) {
String s = "abcdEFG";
System.out.println(s.toLowerCase());//转小写
System.out.println(s.toUpperCase());//转大写
String s1 = s.concat("HIJK"); //两个字符串拼接 并返回新的字符串对象.
System.out.println(s1);
String s2 = "ab:cd:E:FGH";
String[] array = s2.split(":");//用指定符号 来分割字符串为数组
System.out.println(Arrays.toString(array));
System.out.println(array[3]);
}
spilt()方法中要是以 “.”之类的关键字为分割点,要加入“\. ”将其转义为普通字符,才能用
String a14 = "ab.cd.ef.gh";
String []a15;
a15 =a14.split("\.");
System.out.println(Arrays.toString(a15));//[ab,cd,ef,gh]
替换功能(加了个切割split功能和tirm去除空格功能)
String replace(char old,char new) 将String中的oldChar字符替换为newChar字符
String replace(String old,String new) 将String中的old子字符串替换为new字符串
String trim() 去除字符串两端空格中间的去除不了
public static void main(String[] args) {
String s1="abcdeg";
String s2=s1.replace('a','b');
// String replace(char old,char new) 将String中的oldChar字符替换为newChar字符
System.out.println(s2); //bbcdeg
String s3="abcdeg";
String s4=s3.replace("ab","cd");
// String replace(String old,String new) 将String中的old子字符串替换为new字符串
System.out.println(s4); //cdcdeg
String s5=" abcd eg ";
System.out.println(s5.trim());
//去除字符串两端空格中间的去处不了 String trim()
}
split(String regex) 在字符串中根据传入的regex子字符串根据它的位置将父字符串切割为很多子字符串并把它们放到字符串数组中
replaceAll(String regex, String replacement) 在字符串中将指定的第一个regex子字符替换为replacement字符串
replaceFirst(String regex, String replacement) 在字符串中将指定的第全部regex子字符替换为replacement字符串
public static void main(String[] args) {
String s1 = "ab5cd6ef7gh";
System.out.println(Arrays.toString(s1.split("\d"))); //[ab, cd, ef, gh]
System.out.println(Arrays.toString(s1.split("5"))); //[ab, cd6ef7gh]
// split(String regex) 将字符串按照指定regex(正则表达式的规则)分隔开
String s2="abFGcdFG";
System.out.println(s2.replaceFirst("FG","ab")); //ababcdFG
System.out.println(s2.replaceAll("FG","ab")); //ababcdab
// replaceAll(String regex, String replacement)
//在字符串中将指定的第一个regex子字符替换为replacement字符串
// replaceFirst(String regex, String replacement)
//在字符串中将指定的第全部regex子字符替换为replacement字符串
}



