Java基础篇-Spring

一Java中的String,StringBuilder,StringBuffer三者的区别:

1、就是一个常量(fina修饰)和变量的关系。StringBuilder和StringBuffer均为字符串变量,对象的内容可以修改;而String是常量对象一旦产生后就不可以被修改,重新赋值其实是两个对象,重新创建一个新的对象,然后把新的值保存进去。等待gc回收不用的对象。

2、StringBuffer对方法加了同步锁或者对调用的方法加了同步锁,所以是 线程安全的 。StringBuilder并没有对方法进行加同步锁,所以是 非线程安全的 。

3运行速度,或者说是执行速度,在这方面运行速度快慢为:StringBuilder > StringBuffer > String如果经常需要对一个字符串进行修改,例如插入、删除等操作,使用StringBuffer要更加适合一些。

String s="a"; System.out.println(s);//s=a
        s +="b";System.out.println(s);//s=ab 引用地址= 引用地址+堆地址
        String ss="ab";//引用地址=堆地址

        String sss="a"+"b";// sss=ab引用地址=堆地址+堆地址

        ss和sss都指向一个地址“ab”;

        System.out.println(s=="ab");//false;
        System.out.println(ss=="ab");//true

        System.out.println(sss=="ab");//true

        System.out.println(ss==sss);//true

        System.out.println(s==ss);//false

String s =“a”+“b”;等同于 String s = "ab"。此时只创建了一个对象。程序先执行右侧的常量然后赋值给对象"s",对于常量,编译时就直接存储它们的字面值而不是它们的引用 。
在编译时就直接讲它们连接的结果提取出来变成了"abcde"

s+="c";System.out.println(s);//abc

此时创建了一个新的对象也名为“s“有个空间是没有指向的,也不会被回收,因为它们都还不是Null.只有等到代码结束的时候才会被回收。String

向StringBuffer中赋值的时候可以通过它的append方法.      sb.append("hello");

二String的常用方法:

1、String []    split(String s);

String s="abcd,abc";

String [] arr=s.split(",");根据","去截取字符串分为“abcd”和“abc”俩个对象。

2、public String substring(int beginIndex)以0开始计数,从起始索引(包括)截取到最后;

public String substring(int beginIndex, int endIndex)结束索引(不包括);从起始索引(包括)截取到结束索引-1;开始和结束均为字符位置。

public class StringTest {
    public static void main(String args[]) {
        String St = new String("www.runoob.com");
 
        System.out.print("返回值 :" );
        System.out.println(St.substring(4) );//返回值 :runoob.com
 
        System.out.print("返回值 :" );
        System.out.println(St.substring(4, 10) );//返回值 :runoob
    }

}

3 startsWith() 方法用于检测字符串是否以指定的前缀开始


语法
public boolean startsWith(String prefix, int toffset)

public boolean startsWith(String prefix)
参数
    prefix -- 前缀。
    toffset -- 字符串中开始查找的位置。
返回值
如果字符串以指定的前缀开始,则返回 true;否则返回 false。
实例
public class Test {
    public static void main(String args[]) {
        String Str = new String("www.runoob.com");
 
        System.out.print("返回值 :" );
        System.out.println(Str.startsWith("www") );
 
        System.out.print("返回值 :" );
        System.out.println(Str.startsWith("runoob") );
 
        System.out.print("返回值 :" );
        System.out.println(Str.startsWith("runoob", 4) );
    }
}
以上程序执行结果为:
返回值 :true
返回值 :false
返回值 :true

4、

语法public String replace(char oldChar,char newChar)

replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。
参数
oldChar -- 原字符。
newChar -- 新字符。
返回值
替换后生成的新字符串。
实例
public class Test {
    public static void main(String args[]) {
        String Str = new String("hello");

        System.out.print("返回值 :" );
        System.out.println(Str.replace('o', 'T'));

        System.out.print("返回值 :" );
        System.out.println(Str.replace('l', 'D'));
    }
}
以上程序执行结果为:

返回值 :hellT
返回值 :heDDo

5、

(1)int  s.indexOf(char); //从前往后查找字符在字符串中出现的第一个位置并返回。返回-1就是没找到

    String st=abcdef;  int i=st.indexOf('d');//i=3

(2)int s.lastIndexOf(char)//返回该字符在字符串中最后一次出现的位置。

(3)int s.indexOf(char,int);//返回该字符在字符串中第几次出现的位置。写超了没事返回最后一个的位置

    String st=abcdefbab;  int i=st.indexOf('b',2);//i=6  

    int i2=st.indexOf('b',3);写超了没事也是6 

(4)int  s.indexOf(String);//返回该字符串在字符串中第一次出现的位置。返回-1就是没找到

    String s="caobod"; int i=s.indexOf("ao");//i=1;

(5))int  s.indexOf(String,int)//返回该字符串在字符串中第几次出现的位置。

(6)int s.lastIndexOf(String)//返回该字符串在字符串中最后一次出现的位置。

(7)int lastIndexOf(char, fromIndex);//从指定的索引处从后往前找该字符在此字符串中最后一次出现处的索引,比如是指定索引为7,就从7前面的索引找第一次找到则返回索引值

(8)int lastIndexOf(String, fromIndex);//从指定的索引处从后往前找该字符串在此字符串中最后一次出现处的索引。










猜你喜欢

转载自blog.csdn.net/luguodehua/article/details/80203004