java sting类,Java的String和StringBuilder类

1 String类简介

String类的特性:

1) Java.lang.String使用了final修饰,不能被继承;

2) 字符串底层封装了字符数组及针对字符数组的操作方法;

3)字符串一旦创建,对象永远无法改变,但字符串引用可以重新赋值;

字符串不能被改变的原因:

1)Java为了提高性能,静态字符串在常量池中创建,并尽量使用同一个对象,重用静态字符串;

2)对于重复出现的字符串直接量,JVM会首先在常量池中查找,如果存在即返回该对象。

String类的内存编码和长度:

String在内存中采用Unicode编码,每个字符占用两个字节;任何一个字符(无论是中文还是英文)都算1个字符长度,占用2个字节。

测试字符串长度的实例:

package com.example.string;

public class StringClass {

public void testLength(){

String str1="hello";

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

String str2="中国";

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

}

public static void main(String[] args) {

StringClass sc = new StringClass();

sc.testLength();

}

}

//输出的结果

5

2

说明无论中文还是英文一个字符都是一个长度

String类中的常用方法:

String类之indexOf方法:字符串检索

2cef9dc576c05cc4d90e80f01677934d.png

indexOf的实例:

package com.example.string;

public class StringIndexOf {

public static void main(String args[]) {

String oneStr = newString("chinaokokok");

String twoSubStr = new String("ok");

String threeSubStr = new String("ko");

System.out.print("查找字符 o 第一次出现的位置 :" );

System.out.println(oneStr.indexOf( 'o'));

System.out.print("子字符串 twoSubStr 第一次出现的位置:" );

System.out.println( oneStr.indexOf( twoSubStr ));

System.out.print("从第2个位置开始搜索子字符串 twoSubStr 第一次出现的位置 :" );

System.out.println( oneStr.indexOf( twoSubStr, 2));

System.out.print("从第7个位置开始搜索子字符串 threeSubStr 最后一次出现的位置 :" );

System.out.println(oneStr.lastIndexOf( threeSubStr,7));

}

}

//输出结果为:

查找字符 o 第一次出现的位置 :5

子字符串 twoSubStr 第一次出现的位置:5

从第2个位置开始搜索子字符串 twoSubStr 第一次出现的位置 :5

从第7个位置开始搜索子字符串 threeSubStr 最后一次出现的位置 :6

String类之substring方法:取子字符串:

bd00d1520e377b57219ca0a6d0a11329.png

代码实例:

package com.example.string;

public class StringSubstring {

public static void main(String[] args) {

String oneStr = "starbegining";

System.out.println(oneStr.substring(1));

System.out.println(oneStr.substring(1,5));

}

}

//输出结果

tarbegining

tarb

String类之trim方法:去掉字符串的前导和后继空白

package com.example.string;

public class StromgTrim {

public static void main(String[] args) {

String name = " li ";

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

}

}

//输出结果为:

li

String类之charAt方法:

char charAt(int index): 方法charAt()用于返回字符串指定位置的字符。

代码实例:

package com.example.string;

public class StringCharAt {

public static void main(String[] args) {

String lable = "world";

System.out .println(lable.charAt(2));

}

}

//输出结果为:

r

String类之startsWith和endsWith方法:

e5458f30556c74346a9bde95e721a8fe.png

代码实例:

package com.example.string;

public class StringWith {

public static void main(String[] args) {

String name = "hello";

System.out.println(name.startsWith("h"));

System.out.println(name.startsWith("e", 1));

System.out .println(name.endsWith(""));

}

}

//输出结果为:

true

true

true

String类之toUpperCase和toLowerCase方法:中英文大小写转换

package com.example.string;

public class StringToCase {

public static void main(String[] args) {

String name = "世界Hello";

System.out.println(name.toUpperCase());

System.out.println(name.toLowerCase());

}

}

//输出结果为:

世界HELLO

世界hello

String类之valueOf方法:

返回参数的字符串类型,参数可以是:布尔类型,字符类型,double类型,float类型,int类型,long类型

package com.example.string;

public class StringValueOf {

public static void main(String[] args) {

double a = 22.33;

boolean b = false;

long c = 22l;

char[] tt = {'a','b'};

System.out .println(String.valueOf(a));

System.out .println(String.valueOf(b));

System.out .println(String.valueOf(c));

System.out .println(String.valueOf(tt));

}

}

//输出结果为:

22.33

false

22

ab

2 StringBuilder类简介

StringBuilder类的特性:

1) StringBuffer类的对象能够被多次的修改,并且不产生新的未使用对象。

2)StringBuilder 类在 Java 5 中被提出, StringBuilder 的方法不是线程安全的(不能同步访问)。

3)由于 StringBuilder有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则不能选用。

StringBuilder类的构造函数:

public StringBuilder();

public StringBuilder(String str);

StringBuilder类的常用方法:

b9c1bd7b85a99249ffab645ada357f67.png

补充:StringBuiler并不是只有这些方法,以上只是比String类多出的一些特别方法,其余方法大部分跟String类的方法类似,所以一般可以用StringBuiler替代String,原因见StringBuiler类的特性。

StringBuilder append(String str)方法:

package com.example.string;

public class StringBuilderAppend {

public static void main(String[] args) {

StringBuilder stb = newStringBuilder("hello");

System.out .println(stb.append(" world"));

}

}

//输出结果为:

hello world

StringBuilder insert(int dstOffset, String str)方法:

package com.example.string;

public class StringBuilderInsert {

public static void main(String[] args) {

StringBuilder stb = newStringBuilder("hello");

System.out .println(stb.insert(1, "HH"));

}

}

//输出结果为:

hHHello

StringBuilder delete(int start, int end)方法:

package com.example.string;

public class StringBuilderDelete {

public static void main(String[] args) {

StringBuilder stb = newStringBuilder("hello");

System.out .println(stb.delete(4, 4+1));

}

}

//输出结果为:

hell

StringBuilder replace(int start, int end, String str)方法:

package com.example.string;

public class StringBuilderReplace {

public static void main(String[] args) {

StringBuilder stb = newStringBuilder("hello");

System.out .println(stb.replace(0, 2, "wt"));

}

}

//输出结果为:

wtllo

StringBuilder reverse()方法:

package com.example.string;

public class StringBuilderReverse {

public static void main(String[] args) {

StringBuilder stb = newStringBuilder("hello");

System.out .println(stb.reverse());

}

}

//输出结果为:

olleh

3 延伸:StringBuilder 和StringBuffer

作用:

1)StringBuffer也是一个关于字符串处理的类

2)StringBuffer和StringBuilder类拥有的方法基本相同,但是两者底层原来的区别如下:

3)StringBuffer是线程安全的,同步处理的,性能稍慢,如果需要线程安全的情况下要用StringBuffer。

4)StringBuilder是非线程安全的,并发处理的,性能稍快,一般建议用StringBuilder

以上代码可以通过地址“ https://github.com/gongyunit/j2eecode ”来获取。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值