# 基本内容
1.String类如何判断是否相等
2.字符串的子串
3.如何连接字符串等等
一.简单介绍String类
String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。
二、String类对象的创建
字符串声明形式为: String stringName;
字符串创建方式为:stringName = new String(字符串常量);
或stringName = 字符串常量;
三.String类的构造方法
1、public String()
注:这是无参构造方法,用来创建空字符串的String对象。例:
String str1 = new String();
2.public String(String value)
注:这是用已知的字符串value创建一个String对象。例:
String str2 = new String("LIXIN");
String str3 = new String(str2);
3、public String(char[] value)
注:这是用字符数组value创建一个String对象。例:
char[] value = {"a","b","c","d"};
String str4 = new String(value);//相当于String str4 = new String("abcd");
4、public String(char chars[], int startIndex, int numChars)
注:这是用字符数组chars的startIndex开始的numChars个字符创建一个String对象。
char[] value = {"a","b","c","d"};
String str5 = new String(value, 1, 2);//相当于String str5 = new String("bc");
5、public String(byte[] values)
注:这是用比特数组values创建一个String对象。例:
byte[] strb = new byte[]{65,66};
String str6 = new String(strb);//相当于String str6 = new String("AB");
有这些构造方法有:
1、String类是通过char数组保存字符串。
2、String类是用final修饰的,可说明String不可继承。其次其中字段value是char数组,也是用final修饰的,final修饰的字段创建后就不可变。因此,String对象一旦被创建就是固定不变的,String类对象有编辑字符串的功能,比如replace(),这些编辑功能是通过创建一个新的对象来实现的,而不是对原有对象进行修改,因此其任何改变都不会影响到原对象,但相关的其他改变操作都会生成新的对象。
备注1:String类的value属性用final修饰,代表value的引用地址不可变,但Array变量只是stack上的一个引用,数组的本体结构在堆,堆里Array中数据可变,即Array数组是可变的。例如:
final int[] value = {1,2,3};
value[0] = 4; //这时数组里的第一个元素1已变为4
四.常用方法以及相应的代码
1.字符串比较判断
(1)、对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
形式:
public int compareTo(String anotherString)
(2)、与compareTo方法相似,但忽略大小写。
形式:
public int compareToIgnore(String anotherString):
(3)、比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
形式:
public boolean equals(Object anotherObject)
(4)、与equals方法相似,但忽略大小写。
形式:
public boolean equalsIgnoreCase(String anotherString)
以上的实例有:
package abcd;
public class efgh{
public static void main(String[] args) {
String str1 = new String("abc");//创建字符串
String str2 = new String("ABC");//创建字符串
int a = str1.compareTo(str2);//情况(1)a>0
int b = str1.compareToIgnoreCase(str2);//情况(2)b=0
boolean c = str1.equals(str2);//情况(3)c=false
boolean d = str1.equalsIgnoreCase(str2);//情况(4)d=true
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str2));
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
输出结果有:
2.提取字符串子串用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:
(1).该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
形式:
public String substring(int beginIndex)
(2).该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
形式:
public String substring(int beginIndex, int endIndex)
以上的实例有:
package abcd;
public class efgh { public static void main(String[] args) {
String str1 = new String("lixindabendan");//创建
String str2 = str1.substring(5);//情况(1)str2 = "dabendan"
String str3 = str1.substring(0,5);//情况(2)str3 = "lixin"
System.out.println(str2);
System.out.println(str3);
}
}
输出结果有:
3.字符串连接
将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
形式:
public String concat(String str)
代码实例有:
package abcd;
public class efgh { public static void main(String[] args) {
String str1 = "lixin".concat("shi").concat("shabi");
//相当于
String str2 = "lixin"+"shi"+"shabi";
System.out.println(str1);
System.out.println(str2);
}
}
结果有:
4.求字符串长度
返回该字符串的长度
形式:
public int length()
代码实例有:
package abcd;
public class efgh {public static void main(String[] args) {
String str = new String("shabilixin");
int strlength = str.length();//strlength = 10
System.out.println(str);
System.out.println(strlength);
}}
5.字符串某一位置字符返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个length()-1。序列的第一个 char 值位于索引 0 处,第二个位于索引 1 处,依此类推,这类似于数组索引。
形式:
public char charAt(int index)
代码实例有:
package abcd;
public class efgh { public static void main(String[] args) {
String str = new String("lixin4shabi");
char ch = str.charAt(5);//ch = 6
System.out.println(str);
System.out.println(ch );
}}
6.字符串中单个字符查找
(1).用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
形式:
public int indexOf(int ch/String str)
(2).改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
形式:
public int indexOf(int ch/String str, int fromIndex)
(3).该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
形式:
public int lastIndexOf(int ch/String str)
(4).该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
形式:
public int lastIndexOf(int ch/String str, int fromIndex)
代码实例有:
package abcd;
public class efgh {public static void main(String[] args) {
String str = "lixin is a good shabi";
int a = str.indexOf('a');//a = 9
int b = str.indexOf("good");//b = 11
int c = str.indexOf("s",2);//c = 7
int d = str.lastIndexOf("b");//d = 19
int e = str.lastIndexOf("i",2);//e = 1
System.out.println(str);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}}
7.字符串中字符的大小写转换
(1).返回将当前字符串中所有字符转换成小写后的新字符串
形式:public String toLowerCase()
(2).返回将当前字符串中所有字符转换成大写后的新字符串
形式:public String toUpperCase()
代码实例有:
public class efgh { public static void main(String[] args) {
String str = new String("lixinSHABI");
String str1 = str.toLowerCase();//str1 = "lixinshabi"
String str2 = str.toUpperCase();//str2 = "LIXINSHABI"
System.out.println(str);
System.out.println(str1);
System.out.println(str2);
}}
8.字符串中字符的替换
(1).用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
形式:public String replace(char oldChar, char newChar)
(2).该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
形式:public String replaceFirst(String regex, String replacement)
(3).该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
形式:public String replaceAll(String regex, String replacement)
代码实例有:
package abcd;
public class efgh { public static void main(String[] args) {
String str = "lixinshabi";
String str1 = str.replace('l','g');//str1 = "gixinshabi"
String str2 = str.replace("lixin","wangyi");//str2 = "fghzxcfgh"
String str3 = str.replaceFirst("lixin","zhangtengbo");//str3 = "fghzxcasd"
String str4 = str.replaceAll("lixin","liutao");//str4 = "fghzxcfgh"
System.out.println(str);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
}}
输出结果有:
9.截去字符串两端的空格,但对于中间的空格不处理。返回字符串的副本,忽略前导空白和尾部空白
形式:public String trim()
代码实例:
package abcd;
public class efgh { public static void main(String[] args) {
String str = " a sd ";
String str1 = str.trim();
int a = str.length();//a = 6
int b = str1.length();//b = 4
System.out.println(str);
System.out.println(str1);
System.out.println(a);
System.out.println(b);
}}
输出结果有:
10.用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。
形式:public boolean statWith(String prefix)public boolean endWith(String suffix)
代码实例有:
package abcd;
public class efgh { public static void main(String[] args) {
String str = "lixinshishabiduima";
boolean a = str.startsWith("lixinshishabi");//a = true
boolean b = str.endsWith("duima");//b = true4
System.out.println(str);
System.out.println(a);
System.out.println(b);
}}
输出结果有:
11.从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。
形式:public regionMatches(boolean b, int firstStart, String other, int otherStart, int length)
12、判断参数s是否被包含在字符串中,并返回一个布尔类型的值。
形式:public contains(String str)
代码实例为:
package abcd;
public class efgh {
public static void main(String[] args) {
String str = "lixinzuishuai";
str.contains("lixin");//true
str.contains("shabi");//false
System.out.println(str.contains("lixin"));
System.out.println(str.contains("shabi"));
}
}
输出结果为:
13.将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。
形式:public String[] split(String str)
代码实例有:
package abcd;
public class efgh {
public static void main(String[] args) {
String str = "asd!qwe|zxc#";
String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";
System.out.println(str);
System.out.println(str1[0]);
System.out.println(str1[1]);
System.out.println(str1[2]);
}
}
14.测试字符串是否以str结尾
形式:public boolean endsWith(String str)
15.将字符串转换成字符数组返回
形式:public char[] getBytes
16.将指定的字符串转成制服数组返回
形式:public char[] getBytes(String str)
17.测试字符串是否以str开始
形式:public boolean startsWith(String str)
18.将字符串转换成字符数组
形式:public char[] toCharArray
19.将指定内容用字符串表示
(1).将布尔方法b的内容用字符串表示
形式:public String valueOf(Boolean b)
(2).将字符ch的内容用字符串表示
形式:public String valueOf(char ch)
(3).将数字index的内容用字符串表示
形式:public String valueOf(int index)
(4).将长整数字l的内容用字符串表示
形式:public String valueOf(long l)
20.根据给定正则表达式的匹配拆分此字符串。
形式:public String[] split(String regex)
21.字符串转换为基本类型
java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:
形式:public static byte parseByte(String s)//情况1
public static short parseShort(String s)//情况2
public static short parseInt(String s)//情况3
public static long parseLong(String s)//情况4
public static float parseFloat(String s)//情况5
public static double parseDouble(String s)//情况6
代码说明有:
int n = Integer.parseInt("12");
float f = Float.parseFloat("12.34");
double d = Double.parseDouble("1.124");
22.基本类型转换为字符串类型
形式:
public static String valueOf(char data[])//情况1
public static String valueOf(char data[], int offset, int count)//情况2
public static String valueOf(boolean b)//情况3
public static String valueOf(char c)//情况4
public static String valueOf(int i)//情况5
public static String valueOf(long l)//情况6
public static String valueOf(float f)//情况7
public static String valueOf(double d)//情况8
代码说明有:
String s1 = String.valueOf(12);
String s1 = String.valueOf(12.34);
23.进制转换
举例:
public Long.toBinaryString(long l)
public Long.toOctalString(long l)
public Long.toHexString(long l)
public Long.toString(long l, int p)//p作为任意进制
大概的String类的用法都已经类举,作业完成!