java字符串 字符_Java字符串String详解

-----------siwuxie095

1、String字符串

实例化String对象:

(1)直接赋值,如:String str="hello";

(2)使用关键字

new,如:String str=new String("hello");

6d515cc4ec6b77b70e0f6a0373e48be0.png

由图可知:使用new的方式在堆内存中开辟了两个空间,

第一个 "hello",对象 str 没有指向,无用等待回收,

第二个 "hello",被

str 指向,有用。

所以直接赋值(只开辟了一个空间) 的方式更常用和合理,

可以节省一些空间

String的内容比较

代码:

packagecom.siwuxie095.string;

public classStringDemo01 {

public staticvoidmain(String[] args) {

String str="hello";

String str1=newString("hello");

//str和str1比较结果

//双等比较的是地址结果为false

System.out.println(str==str1);

//equals()比较的是内容结果为true

System.out.println(str.equals(str1));

}

}

运行一览:

显然,双等是错误的,而使用方法

equals()是正确的

〔因为双等比较的是内存地址,第一种方式直接赋值只产生了一个地址,

第二种

new 的方式却产生了两个地址,注定地址是不同的,而 equals()

比较的是内容〕

ba307d3ca9dbdb69bdb2487172fe1ccb.png

字符串的内容不可更改

如:

f64c71b1a3031fc42a54dd9a34d2c357.png

代码:

packagecom.siwuxie095.string;

public classStringDemo02 {

public staticvoidmain(String[] args) {

String str="hello";

str=str+" world!";

System.out.println(str);

}

}

运行一览:

"hello"、"world"、"hello world!" 分别开辟了三个空间,

str 指向 "hello" 的连接断开,继而指向 "hello world!",

即 "hello" 本身没有变化,变的只是

str 中对堆内存地址

的指向

982f5619612ec4865c7fc251cf52d9bd.png

2、String字符串常用方法

String字符串的方法较多,可以根据API给出的方法去做测试,下面是常用方法:

(1)字符串长度:length()「数组中的是 length 属性」

(2)字符串转换数组:toCharArray()

(3)从字符串中取出指定位置的字符:charAt()

(4)字符串与byte数组的转换:getBytes()

(5)过滤字符串中存在的字符:indexOf()

(6)去掉字符串的前后空格:trim() 「空格易影响到对字符串的判断,需适时去掉」

(7)从字符串中取出子字符串:subString()

(8)大小写转换:toLowerCase() toUpperCase()

(9)判断字符串的开头结尾字符:startWith() endsWith()

(10)替换String字符串中的一个字符:replace()

代码:

packagecom.siwuxie095.array;

public classStringDemo03 {

public staticvoidmain(String[] args) {

String str="siwuxie095";

//(1)

System.out.println(str.length());

//(2)

chardata[]=str.toCharArray();

for(inti = 0; i < data.length; i++) {

System.out.print(data[i]+" ");

}

//(3)

System.out.println("\n"+str.charAt(0));

//(4)

bytebytes[]=str.getBytes();

for(inti = 0; i < bytes.length; i++) {

System.out.println(newString(bytes));

}

//(5)若存在,返回当前字符的位置,否则返回-1

System.out.println(str.indexOf("x"));

//(6)

str=" siwuxie095";

System.out.println(str.trim());

//...

}

}

3、StringBuffer

StringBuffer,是String的缓冲区,本身也是操作字符串用的,

但与String不同,StringBuffer是可以更改的

〔String是不可更改的,每次看到更改内容,其实都是在开辟新的

内存空间,非常耗资源,此时可以使用StringBuffer〕

StringBuffer是一个操作类,所以必须通过实例化进行操作

代码:

packagecom.siwuxie095.string;

public classStringBufferDemo01 {

public staticvoidmain(String[] args) {

StringBuffer sb=newStringBuffer();

//通过append()方法添加内容(追加)

sb.append("hello");

//使用toString()将之转换成String类型

//毕竟String和StringBuffer不是同一类型

System.out.println(sb.toString());

tell(sb);

System.out.println(sb.toString());

}

public staticvoidtell(StringBuffer s) {

s.append(" world!");

}

}

运行一览:说明StringBuffer是可以更改的

2b8244a99eb6611fbb88795341839a06.png

对比:

packagecom.siwuxie095.string;

public classStringBufferDemo02 {

public staticvoidmain(String[] args) {

String sb="hello";

System.out.println(sb);

tell(sb);

System.out.println(sb);

}

public staticvoidtell(String s) {

s=" world!";

}

}

运行一览:说明String是不可更改的

a03041b339a368f714322e6d9b60c6c1.png

StringBuffer的常用方法:

append() insert() replace() indexOf()

代码:

packagecom.siwuxie095.string;

public classStringBufferDemo03 {

public staticvoidmain(String[] args) {

StringBuffer sb=newStringBuffer();

sb.append("hello");

sb.insert(0, "hi");

System.out.println(sb.toString());

sb.replace(1, 4, "www");

System.out.println(sb.toString());

}

}

运行一览:

da0938f87fa428b1edcf71823ddee127.png

StringBuffer类的应用:

为什么有的时候要用StringBuffer,而不用String:

代码:

packagecom.siwuxie095.string;

public classStringBufferDemo04 {

public staticvoidmain(String[] args) {

String str="hello";

for(inti = 0; i < 100; i++) {

//会自动把int型的i转换为String类型

//每加一次就会开辟一个空间来储存新的

//最后开辟了100个,耗费资源

str=str+i;

}

System.out.println(str);

//使用StringBuffer不会耗费资源,且会更快

//所以很多时候使用StringBuffer

StringBuffer sb=newStringBuffer();

sb.append("hello");

for(inti = 0; i < 100; i++) {

sb.append(i);

}

System.out.println(sb);

}

}

4、StringBuilder

一个可变的字符序列,该类被设计作用StringBuffer的一个简易替换,

用在字符串缓冲区被单个线程使用的时候,建议优先考虑该类,速度

比StringBuffer要快

但如果涉及到线程安全方面,建议使用StringBuffer

常用方法:

append() insert() …

【made by siwuxie095】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值