String类
实例化Stirng对象的两种方式:
A、直接赋值
public class StringDemo01
{
public static void mian(String args[]){
String name="张三"; //直接为String赋值
System.out.println("姓名:"+name);
}
}
B、通过关键字new赋值
public class StringDemo01
{
public static void mian(String args[]){
String name=new String("张三"); //使用new关键字进行赋值
System.out.println("姓名:"+name);
}
}
关于两者之间的不同,看如下代码分析:
public class StringDemo02
{
public static void main(String args[]){
String name1="张三";
String name2="张三";
String name3=new String("张三");
String name4=new String("张三");
System.out.println("equals方法的比较:"+name1.equals(name3)); //-->true
System.out.println("==的比较:"+name1==name2); //-->true
System.out.println("==方法的比较:"+name1==name3); //-->false
System.out.println("==方法的比较:"+name3==name4); //-->false
}
}
equals方法与==的区别:
在java中,str1.equals(str2)方法比较的是两个字符串是否相等,而==,表示的是只有两个引用都指向了同一个对象时才会返回true;
String直接赋值和使用new关键字两种实例化方式的区别:
1、采用直接赋值的方式其实JVM自动创建了一个匿名对象,当为String直接赋值的时候会自动在堆内存中进行查找,如果在池中找到了值为”张三”的字符串,就不会再重新开辟空间,直接使用当前堆内存的中的字符串的值。所以name1==name2返回为true。
2、而当使用new关键字的时候,都会在堆中创建,此时开辟的就是两个空间,其中有一个是垃圾的空间,需要等待垃圾的回收。
综上所述,在java的开法中通常是使用为String直接赋值的方式,创建相对较少的对象,从而节省内存资源。
·字符串的内容不能改变
String一旦声明则不能改变其值,原因是String本身是一个类,而在java中类属于引用数据类型,这一点较为重要,如果想要改变字符串的值,则最好使用StringBuffer类。
代码解释:
使用String 值不会被改变
public class StringDemo02
{
public static void main(String args[]){
String name1="张三";
change(name1);
System.out.println(name1); //结果 张三
}
public static void change(String str){
str="你好";
}
}
使用StringBuffer 值会被改变:
public class StringDemo02
{
public static void main(String args[]){
StringBuffer name1=new StringBuffer("张三");
change(name1);
System.out.println(name1); //结果 张三你好
}
public static void change(StringBuffer str){
str.append("你好");
}
}
String类的常用方法
String类的常用方法:
·字符数组与字符串
一个字符串可以变为一个字符数组,同样,也可以把一个字符数组,变为一个字符串。
在String类中提供了一下的操作方法:
·将字符串变为字符数组:public char[] toCharArray()
public class StringApiDemo01
{
public static void main(String args[]){
String a="hello";
char c[]=a.toCharArray(); //将字符串变为字符数组
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}
}
}
·将字符数组变为字符串:
1、 采用构造方法的形式:
·public String(char[] value)
·String(char[] value, int offset, int count) 根据字符数组的开始点以及长度转为字符串
2、 采用方法的形式:
public static void valueOf(char[] data)
public class StringApiDemo02
{
public static void main(String args[]){
char c[]={'h','e','l','l','o'};
String str1=new String(c); //将字符数组变为字符串
String str2=new String(c,0,3); //从数组的第0个索引取3个长度转为字符串
String str3=new String().valueOf(c); //使用方法返回字符串
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
·从字符串中取出指定位置的字符
方法:public char charAt(int index)
public class StringApiDemo02
{
public static void main(String args[]){
String str="hello"; //定义字符串
System.out.println(str.charAt(3)); //取出第四个字符
}
}
·字符串与byte数组的转换
·将byte数组变为字符串:public byte[] getBytes()
|-:将一个字节数组变为字符串:public String (byte[] bytes)
|-:将部分字节数组变为字符串:public String (byte[] bytes,int offset,int length)
public class StringApiDemo03
{
public static void main(String args[]){
String str="hello"; //定义字符串
byte b[]=str.getBytes();
String str1=new String(b); //将byte数组变为字符串
String str2=new String(b,0,3); //将部分byte数组变为字符串
System.out.println(str1);
System.out.println(str2);
}
}
·取得一个字符串的长度
方法:public int lenth()
public class StringApiDemo04
{
public static void main(String args[]){
String str="hello"; //定义字符串
System.out.println("\""+str+"\"的长度为:"+str.length());
}
}
注意:数组的长度为属性,即:数组名.length
字符串的长度为方法,即:字符串.length()
·查找指定的字符串是否存在
·从头开始查找:public int indexOf(String str)
·从指定位置开始查找:public int indexOf(String str,int fromIndex)
·扩展:指定字符在此字符串中最后一次出现处的索引。public int lastIndexOf(int ch)
两种查找的方式最终都会返回int类型的数据,此数据表示的是一个字符串的具体位子,如果没有查找到此字符串,则会返回-1
public class StringApiDemo05
{
public static void main(String args[]){
String str="hellcollc"; //定义字符串
System.out.println(str.indexOf("c")); //从头开始查找c
System.out.println(str.indexOf("c",5)); //从指定位置开始查找c
System.out.println(str.indexOf("x")); //查找x 若没有 则会返回-1
System.out.println(str.lastIndexOf("c")); //查找c最后一次出现的索引位置
}
}
·去掉空格
假如对用于的输入进行去空格的操作,此种方法只是会去掉字符串左右两边的空格,中间的空格是不能被去掉的。
public class StringApiDemo06
{
public static void main(String args[]){
String str=" hello "; //定义字符串
System.out.println(str); //输出带空格
System.out.println(str.trim()); //输出没有空格
}
}
·字符截取
·从指定位置开始截取到结束位置:public String substring(int beginIndex)
·截取指定范围的字符串:public String substring(int beginIndex,int endIndex)
public class StringApiDemo07
{
public static void main(String args[]){
String str="hello world"; //定义字符串
System.out.println(str.substring(5)); //从第6个位置开始截取直到最后
System.out.println(str.substring(3,6)); //从第4个位置开始截取,到低7个位置
}
}
注意:假如此时指定索引超出字符串的大小,则会抛出java.lang.StringIndexOutOfBoundsException 该异常继承自IndexOutOfBoundsException。
·拆分字符串
如果需要按指定的字符串去拆分另一个字符串:public String[] split(String regex)
代码示例:按指定字符进行拆分,并赋值给一个新的字符串
public class StringApiDemo08
{
public static void main(String args[]){
String str="hello world"; //定义字符串
String str1=""; //定义一个字符串用来接收
String s[]=str.split(" "); //按空格进行拆分
for(String x:s){ //循环输出
str1+=x; //每循环一次为str1添加一个字符
System.out.print(x);
}
System.out.println(str1); //得到str1的值
}
}
·大小写转换
此功能在开发中会经常用到,将一个字符串全部字母变为小写或者将一个小写的字符串中的全部字母全部变为大写。
·大写变小写:public void toUpperCase()
·小写变大写:public void toLowerCase()
public class StringApiDemo09
{
public static void main(String args[]){
System.out.println("将\"hello\"转大写"+"hello".toUpperCase());
System.out.println("将\"HELLO\"转小写:"+"HELLO".toLowerCase());
}
}
·判断是否以指定的字符串开头或结尾
·判断是否以指定的字符串开头:public boolean startsWith(String prefix);
·判断是否以指定的字符串结尾:public boolean endsWith(String suffix);
public class StringApiDemo10
{
public static void main(String args[]){
String str="**hello";
String str1="hello**";
System.out.println(str.startsWith("*")); //判断是否以*开头 true
System.out.println(str1.endsWith("*")); //判断是否以*结尾 true
}
}
·不区分大小写的比较
在String类中equals()方法是用来比较字符串是否相等,但是此种方法只是针对大小写一样的字符串来说的,如果不想区分大小写来进行比较,可以采用以下方法:
public boolean equalsIgnoreCase(String anotherString)
public class StringApiDemo11
{
public static void main(String args[]){
String str="HELLO";
String str1="hello";
System.out.println("\""+str+"\"和"+"\""+str1+"\"区分大小写的比较:"+str.equals(str1));
System.out.println("\""+str+"\"和"+"\""+str1+"\"不区分大小写的比较:"+str.equalsIgnoreCase(str1));
}
}
·字符串的替换功能
将字符串按照指定的格式进行全部替换:
public class StringApiDemo12
{
public static void main(String args[]){
String str1="hello";
String str2=str1.replaceAll("l","x"); //将字符串中的所有l替换为x
System.out.println(str2);
}
}
this关键字
this关键字的作用:
1、 表示本类或者是父类的方法
2、 表示类中的属性
3、 可以使用this调用本类中的构造方法
4、 this表示当前对象
表示类中的属性
class Person
{
private String name; //姓名
private int age; //年龄
public Person(String name,int age){ //构造方法,为Person类中的属性初始化值
this.name=name;
this.age=age;
}
public void getInfo(){
System.out.println("姓名是: "+name+"年龄是: "+age);
}
}
public class thisDemo01
{
public static void main(String args[]){
Person person=new Person("张三",20);
person.getInfo();
}
}
使用this调用构造方法
如果在一个类中有多个构造方法的话,也可以使用this关键字互相调用。
class Person
{
private String name; //姓名
private int age; //年龄
public Person(){
System.out.println("新的对象被实例化");
}
public Person(String name){
this(); //调用无参的构造方法
this.name=name;
}
public Person(String name,int age){ //构造方法,为Person类中的属性初始化值
this(name); //调用有一个参数的构造方法
this.age=age;
}
public void getInfo(){
System.out.println("姓名是: "+name+"年龄是: "+age);
}
}
public class thisDemo02
{
public static void main(String args[]){
Person person=new Person("张三",20);
person.getInfo();
}
}
注意:在使用this关键字调用其它构造方法时,有以下的限制:
·this调用构造方法的语句只能放在构造方法的首行。
·在使用this调用本类中其它构造方法时,至少有一个构造方法是不能用this调用的,原因是构造方法出现了递归调用,此时程序会出现错误。
this关键字表示当前对象
当前对象:当前正在调用方法的对象
class Person
{
public String getInfo(){
System.out.println("Person类:"+this); //直接打印this
return null; //为保证语句正确,直接返回null
}
}
public class thisDemo03
{
public static void main(String args[]){
Person per1=new Person(); //通过构造方法实例化对象
Person per2=new Person(); //通过构造方法实例化对象
System.out.println("MAIN方法:"+per1); //直接打印对象
per1.getInfo(); //当前调用getInfo方法的是per1对象
System.out.println("MAIN方法:"+per2); //直接打印对象
per2.getInfo(); //当前调用getInfo方法的是per2对象
}
}
对象比较
可以使用this和引用传递进行两个对象是否相等的判断。
class Person
{
private String name;
private int age;
public Person(String name,int age){
this.setName(name);
this.setAge(age);
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public boolean compare(Person per){ //直接传递本类对象,方便的调用类中的私有属性
//地址值相等,则两个对象肯定是相等的
if(this==per){
return true;
}
//地址值假如对象不相等 则判断各自对象的属性值是否相等
else if(this.name.equals(per.name)&&this.age==per.age){
return true;
}
else{
return false;
}
}
}
public class thisDemo04
{
public static void main(String args[]){
Person per1=new Person("张三",20); //通过构造方法实例化对象
Person per2=new Person("张三",30); //通过构造方法实例化对象
if(per1.compare(per2)){
System.out.println("相等!");
}
else{
System.out.println("不相等!");
}
}
}