字符串的处理方法总结

String类和StringBuffer类

1、String处理一些小的文本

StringBuffer处理大型文本

原因:用String处理大型文本,会消耗大量系统资源。

2、String类一旦产生字符串,某对象不可变。

虽通过各种系统方法操作字符串,但不改变对象本身,而产生新的字符串。

StringBuffer类处理可变字符串。

不产生新的字符串。(两种对字符串的操作方法不同)

3、String类

内存分配是按对象包的实际字符分配。

StringBuffer类

内存分配,除当前字符空间所占处,还提供16个字符大小的缓冲区。

字符串处理类——String(共9种)下面是其中6中常用的

格式:字符串类型  字符串名=字符串内容

1、字符串的默认构造器

"String()"  最简单的构造器(不带参数)

class Str

{

public static void main(String[] args)

{

String str = new String();

System.out.println(str);

}

}

2、字节参数构造器

"String(byte[]  byte)"将字节数组中的元素作为字符串对象

class Str

{

public static void main(String[] args)

{

byte[]  b = {97,98,99};

String str = new String(b);

System.out.println(str);

}

}

3、获取指定字节数的构造器

"String(byte[]  byte, int offset, int length)"

构造器含义:将字节数组中从"offset"指定的位置开始到"length"结束,其中间的字符构成字符串对象。

class Str

{

public static void main(String[] args)

{

byte[] b = {97,98,99,100,101,102};

String str = new String(b,3,2);

System.out.println(str);

}

}

4、将字节型数据转换为字符集输出的构造器。

"String(byte[]  byte, int offset, int length,String charsetName)"

字符集一般有"us-ascii","iso-8859-1","utf-8","utf-16be","utf-16le","utf16"等

5、字符数组的构造器

"String(char[] value)"这个构造器和第二个很像

class Str

{

public static void main(String[] args)

{

char[] c = {'w','e','l','c','o','m','e'};

String str = new String(c);

System.out.println(str);

}

}

6、截取字符串内容的构造器

"String(char[] value, int offset, int count)"

含义:江字符数组从"offset"开始,"count"结束,中间的字符连接成字符串。


class Str

{

public static void main(String[] args)

{

char[] c = {'w','e','l','c','o','m','e'};

String str = new String(c,3,4);

System.out.println(str);

}

}

字符串的处理方法:

1、串连接:两种方法

A、使用"+"

B、使用方法函数concat(String str)

2、提取子字符串

方式1、"substring(int beginIndex, int endIndex)"指提取有beginIndex位置开始,可以endIndex为结束位置的字符串

方式2、"substring(int Index)"值提取从Index指定位置开始,一直到字符串最后

3、从字符串中分解字符

char At(int index)

4、获取字符串长度

length()   数组length是属性,字符串length()是方法

5、测试字符串是否相等

方法1、equals(String str)不忽略字符串大小写

方法2、equalsIgnose(String str)忽略字符串大小写

6、查找特定字符串

方法1、index Of(子串内容)

方法2、starts With(子串内容)

方法3、ends With(子串内容)

7、从基本类型转换成字符串

value Of()

8、to String()

1,String str = "小明"‘

System.out.println(str);

2,String str = "小明';

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

区别: 1是将对象作为参数

2是将对象中的字符串提取出来,再输出


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值