String类
字符串是一个常量,一旦被赋值了,其值(地址值)不能被更改
-
推荐的使用方式:
String 变量名 = "xxxx" ;//xxxx代表 的当前String的实例
String类常用的功能:
- 获取功能:
-
int length():获取字符串长度 int length():获取字符串长度 public char charAt(int index);获取指定索引处的字符 public String concat(String str):将指定的字符串和当前字符串进行拼接,获取一个新的字符串 public int indexOf(int ch):返回指定字符第一次出现的索引值 public int lastIndexOf(int ch):返回值指定字符最后一次出现的索引值 public String[] split(String regex): 拆分功能:通过指定的格式将字符串---拆分字符串数组 public String substring(int beginIndex) :从指定位置开始默认截取到末尾 角标从0开始 public String substring(int beginIndex,int endIndex) 从指定位置开始,截取到位置结束(包前不包右) 只能取到endIndex-1处 public static String valueOf(boolean/int/long/float/double/char...Object b) 万能方法,将任意类型转换String类型
public class StringDemo {
public static void main(String[] args) {
String str = "helloworldJavaEE" ;
// public char charAt(int index);获取指定索引处的字符
System.out.println("charAt():"+str.charAt(4));
System.out.println("---------------------------------");
//public String concat(String str):将指定的字符串和当前字符串进行拼接,获取一个新的字符串
//字符串最传统的拼接:直接 定义一个String s = "" ; s+任何数据="xxx" ;
//提供了一个功能
System.out.println("concat:"+str.concat("R").concat("Go"));
System.out.println("-----------------------------------");
// public int indexOf(int ch):返回指定字符第一次出现的索引值
// public int lastIndexOf(int ch):返回值指定字符最后一次出现的索引值
System.out.println("indexOf():"+str.indexOf("o"));
System.out.println("lastIndexOf():"+str.lastIndexOf("o"));
System.out.println("-----------------------------------");
//public String[] split(String regex):
String str2 = "JavaEE-Python-Go-R-C-C#-PHP" ;
//使用"-"进行拆分
String[] strArray = str2.split("-");
for(int x = 0 ; x < strArray.length ; x ++){
System.out.print(strArray[x]+"\t");
}
System.out.println();
System.out.println("-----------------------------------");
/**
* public String substring(int beginIndex) :从指定位置开始默认截取到末尾
* 角标从0开始
* public String substring(int beginIndex,int endIndex)
*/
// String str = "helloworldJavaEE" ;
System.out.println("subString():"+str.substring(5));
System.out.println("subString():"+str.substring(5,9));//worl
System.out.println("-----------------------------------");
//public static String valueOf(基本类型以及Object)
System.out.println("valueOf:"+String.valueOf(100)); //100--->"100"
}
}
- 判断功能
-
public boolean equals(Object anObject):比较两个字符的内容是否相同 (区分大小写) public boolean equalsIgnoreCase(String anotherString):比较两个字符串是否相同(不区分大小写) public boolean startsWith(String prefix):判断字符串是否以指定的内容开头 public boolean endsWith(String suffix):判断字符串是否以指定的内容结尾 boolean isEmpty() 判断字符串是否为空 :若为空,则返回true;否则返回false
public class StringDemo2 {
public static void main(String[] args) {
String s1 = "helloJavaEE" ;
String s2 = "hellojavaee" ;
// public boolean equals(Object anObject):比较两个字符的内容是否相同 (区分大小写)
System.out.println("equals:"+s1.equals(s2));
// public boolean equalsIgnoreCase(String anotherString)
System.out.println("equalsIgnoreCase():"+s1.equalsIgnoreCase(s2));
/**
* public boolean startsWith(String prefix):判断字符串是否以指定的内容开头
* public boolean endsWith(String suffix):判断字符串是否以指定的内容结尾
* boolean isEmpty() 判断字符串是否为空 :若为空,则返回true;否则返回false
*/
System.out.println("startsWith():"+s1.startsWith("hel"));
System.out.println("startsWith():"+s1.startsWith("ak47"));
System.out.println("endsWith():"+s1.endsWith("EE"));
s1 = "" ; //length()---->长度0 (空字符序列)
System.out.println(s1.isEmpty());
}
}
- String类的常用的转换功能: (重点)
-
byte[] getBytes() :将字符串转换成字节数组 (编码) 如果方法为空参,使用平台默认的编码集进行编码(utf-8:一个中文对应三个字节) byte[] getBytes(String charset):使用指定的字符集进行编码 解码的过程:将看不懂的字节数----->String String(byte[] bytes):使用默认字符集进行解码 String(byte[] bytes,指定字符集) 编码和解码必须要保证字符集统一 public char[] toCharArray():将字符串转换成字符数组 public String toString():返回自己本身---"当前字符串的内容" public String toUpperCase():将字符串转换成大写 public String toLowerCase():将字符串转换成小写
public class StringDemo {
public static void main(String[] args) {
//测试
String s = new String() ;
System.out.println("s:"+s); //String类重写了Object的toString(),
System.out.println(s.length());
System.out.println("-------------------------------------------");
byte[] bytes = {97,98,99,100,101} ;
// public String(byte[] bytes)
String s2 = new String(bytes) ;
System.out.println(s2);
System.out.println(s2.length());
System.out.println("-------------------------------------------");
//public String(byte[] bytes,int offset,int length):
String s3 = new String(bytes,2,2) ;
System.out.println(s3);
System.out.println(s3.length());
System.out.println("-------------------------------------------");
//public String(char[] value)
char[] chs = {'我','爱','高','圆','圆'} ;
String s4 = new String(chs) ;
System.out.println(s4);
System.out.println(s4.length());
System.out.println("-------------------------------------------");
// public String(char[] value,int offset,int count)
String s5 = new String(chs,1,4) ;
System.out.println(s5);
System.out.println(s5.length());
System.out.println("---------------------------------------------");
// public String(String original)
String s6 = new String("hello") ; //创建字符串对象,常量值:hello
System.out.println(s6);
String s7 = "hello" ; //推荐的方式
System.out.println(s7);
}
}
- 构造方法:
-
public String():空参构造:空字符序列 public String(byte[] bytes):将一个字节数组构造成一个字符串, 使用平台默认的字符集(utf-8:一个中文对应三个字节) 解码 编码和解码---保证字符集统一 编码:将一个能看懂的字符串---->字节 "今天老地方见" utf-8 解码:将看不懂的字节---->字符串 "今天老地方见" gbk public String(byte[] bytes,字符集):使用指定的字符集,将字节数组构造成一个字符串 public String(byte[] bytes,int offset,int length):将指定的部分字节数组转换成字符串 参数1:字节数组对象,参数2:指定的角标值 参数3:指定长度 public String(char[] value):将字符数组构造成一字符串 public String(char[] value,int offset,int count):将部分字符数组转换成字符串 public String(String original):构造一个字符串,参数为字符串常量
public class StringDemo {
public static void main(String[] args) {
//测试
String s = new String() ;
System.out.println("s:"+s); //String类重写了Object的toString(),
System.out.println(s.length());
System.out.println("-------------------------------------------");
byte[] bytes = {97,98,99,100,101} ;
// public String(byte[] bytes)
String s2 = new String(bytes) ;
System.out.println(s2);
System.out.println(s2.length());
System.out.println("-------------------------------------------");
//public String(byte[] bytes,int offset,int length):
String s3 = new String(bytes,2,2) ;
System.out.println(s3);
System.out.println(s3.length());
System.out.println("-------------------------------------------");
//public String(char[] value)
char[] chs = {'我','爱','高','圆','圆'} ;
String s4 = new String(chs) ;
System.out.println(s4);
System.out.println(s4.length());
System.out.println("-------------------------------------------");
// public String(char[] value,int offset,int count)
String s5 = new String(chs,1,4) ;
System.out.println(s5);
System.out.println(s5.length());
System.out.println("---------------------------------------------");
// public String(String original)
String s6 = new String("hello") ; //创建字符串对象,常量值:hello
System.out.println(s6);
String s7 = "hello" ; //推荐的方式
System.out.println(s7);
}
}
-
字符串其他功能:
public String replace(char target,char replacement):替换功能 将指定的内容使用target字符进行替换 public String replaceAll(String regex, String replacement) : 将指定的和参数1正则表达式匹配的字符串 使用replacement进行替换 参数1: [0-9] --->如果字符是数字字符 参数2: "*"替换掉 public String trim():去除字符串两端的空格 重点 public int compareTo(String anotherString):按照字典顺序比较,返回值是int
public class StringDemo2 {
public static void main(String[] args) {
String s = "helloworld" ;
System.out.println("replace():"+s.replace('l','k'));
System.out.println("-------------------------------------");
//public String trim():去除字符串两端的空格
String s2 = "hello";
System.out.println("s2:"+s2+"----");
String s3 = " hello " ;
System.out.println("s3:"+s3);
System.out.println("s3:"+s3.trim()+"----");
System.out.println("------------------------------------");
// public int compareTo(String anotherString):按照字典顺序比较,返回值是int
String str1 = "abc" ;//字典顺序:就是我们26个字母,a,b,c,d,e,f,g,h
String str2 = "hello" ;
System.out.println(str1.compareTo(str2));
String str3 = "hel" ;
System.out.println(str2.compareTo(str3));
}
}