字符串类型属于引用数据类型,Java中用String表示字符串类型。
string是final所修饰的。代表着string这个类不能有子类。(也就是指类中对字符串操作的功能是不能被我们复写)String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。
1.在Java中,字符串其实就是一个String类的对象,可以这样来定义一个字符串:
String str1="HelloJava";//第一种方法
String str2=new String("HelloJava");//第二种方
String类的一些方法
1.判断两个字符相等
public boolean equals(Object anObject)比较源String和anObject内容是否相等,返回值将会是一个boolean值
public static void main(String[] args) {
String str1=new String("HelloJava");
String str2=new String("Hello");
System.out.println(str1.equals(str2));
}
输出结果:false
字符串不相等返回false,字符串相等返回true
equalsIgnoreCase()
public static void main(String[] args) {
String str1=new String("HelloJava");
String str2=new String("hellojava");
System.out.println(str1.equalsIgnoreCase(str2));
}
输出结果:true
字符串不相等返回false,字符串相等返回true
用法类似equals(),只不过比较时忽略大小写
2.求一个字符串的长度
用length方法,返回值是int类型,返回字符串长度
int length()
public static void main(String[] args) {
String str="abcd";
System.out.println(str.length());
}
输出结果:4
3.将指定字符串连接到此字符串的结尾
String concat(String str)
返回值是string类型
public static void main(String[] args) {
String str = "abc";
str = str.concat("123");
System.out.println(str);
}
输出结果:abc123
4.用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:
1)public String substring(int beginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
2)public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
public static void main(String[] args) {
String str1 = new String("abhdhue");
String str2 = str1.substring(2);
String str3 = str1.substring(2,5);
System.out.println(str2);
System.out.println(str3);
}
输出结果:hdhue、hdh
5.判断此字符串是否存在某个字符(串)
boolean contains()
存在在此字符串内返回true,否则返回false
public static void main(String[] args) {
String str = "abc3fg";
System.out.println(str.contains("bc"));
System.out.println(str.contains("o"));
}
输出结果:true 、false
6.根据下标获取字符串的某个字符(可以获取某个特定位置的字符)
char charAt(int index)
返回当前字符串中指定位置的字符
public static void main(String[] args) {
String str="abh4g";
char c=str.charAt(2);
System.out.println (c );
}
输出结果:h
7.把此字符串转换成一个新的char数组
char[] toCharArray()
public static void main(String[] args) {
String str = "This is a String.";
char[] arr = str.toCharArray();
System.out.println(arr);
}
输出结果:This is a String.
8.getBytes()
用平台默认的编码方式对String进行编码,并将结果储存到一个新的byte数组中。
public static void main(String[] args) {
String str1 = "hello";
byte[] array;
array = str1.getBytes();
for(byte x:array){
System.out.println("x="+x);
}
输出结果:x=104
x=101
x=108
x=108
x=111
9.int compareTo()
该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。
若当前对象比参数大则返回正整数,反之返回负整数,相等返回0
public static void main(String[] args) {
String s1 = "abcd";
String s2 = "abce";
String s3 = "Abc";
String s4 = "abcdefg";
System.out.println(s1.compareTo(s2));
System.out.println(s1.compareTo(s3));
System.out.println(s4.compareTo(s1));
System.out.println(s4.compareTo(s2));
}
输出结果:-1、32、3、1
10.将字符串分割成字符串数组
String[] split(String str)
public static void main(String[] args) {
String str = "sgs#jkc#eer";
String[] str1 = str.split("#");
for (int i = 0; i < str1.length; i++) {
System.out.println(str1[i]);
}
输出结果:sgs、jkc、eer
11.替换字符串
public String replace(char oldChar, charnewChar)
//用字符newChar替换当前字符串中所有的oldChar字符,
//并返回一个新的字符串。
public String replaceFirst(String regex,String replacement)
//该方法用字符replacement的内容替换当前字符串中遇到的
//第一个和字符串regex相匹配的子串,应将新的字符串返回。
public String replaceAll(String regex,String replacement)
//该方法用字符replacement的内容替换当前字符串中遇到的所有
//和字符串regex相匹配的子串,应将新的字符串返回。
public static void main(String[] args) {
String str = "hjdfjdskdd";
String str1 = str.replace('h','g');
String str2 =str.replace("hj","xxx");
String str3 =str.replaceFirst("d","c");
String str4 =str.replaceAll("hj","xxx");
System.out.println(str1+"\n"+str2+"\n"+str3+"\n"+str4)
}
输出结果:gjdfjdskdd、xxxdfjdskdd、hjcfjdskdd、xxxdfjdskdd