Java的String、StringBuilder以及StringBuilder和StringBuffer的区别

来源:互联网 发布:西安行知中学初中部 编辑:程序博客网 时间:2024/05/15 10:24

1String类简介


String类的特性:

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

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

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

字符串不能被改变的原因: Java为了提高性能,静态字符串在常量池中创建,并尽量使用同一个对象,重用静态字符串;对于重复出现的字符串直接量,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方法:字符串检索

indexOf的几个常用方法:

 

 int indexOf(int ch)

 

返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1

int   indexOf(String str)

在字符串中检索str,返回其第一次出现的位置,找不到返回-1

int   indexOf(String str, int fromIndex)

从字符串的fromIndex位置开始检索,返回指定str在字符串中第一次出现的索引位置

int   lastIndexOf(String str, int from)

返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索

 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方法:取子字符串:

substring的几个常用方法:

String substring(int beginIndex)

 

从指定位置开始返回一个新的字符串,它是此字符串的一个子字符串。

String substring(int beginIndex, int endIndex)

从指定的开始位置和结束位置获取一个新字符串,它是此字符串的一个子字符串。

 实例:

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方法:

boolean startsWith(String prefix)

测试此字符串是否以指定的前缀开始。

 

boolean startsWith(String prefix, int toffset)

测试此字符串从指定索引开始的子字符串是否以指定前缀开始。

 

boolean endsWith(String suffix)

测试此字符串是否以指定的后缀结束。

 

实例:

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

2StringBuilder类简介 

StringBuilder类的特性:

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

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

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

StringBuilder类的构造函数:

public StringBuilder();

public StringBuilder(String str);

 

StringBuilder类的常用方法:

常用方法

功能描述

append(String   str)

追加字符串

insert(int   dstOffset, String str)

插入字符串

delete(int   start, int end)

删除字符串

replace(int   start, int end, String str)

替换字符串

reverse()

字符串反转

补充: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

作用:

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

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

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

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


以上代码的GitHup地址:https://github.com/gongyunit/j2eecode

阅读全文
0 0
原创粉丝点击