首页 技术 正文
技术 2022年11月19日
0 收藏 690 点赞 4,242 浏览 2603 个字

String

Java中的字符串值属于String类,虽然有其它方法表示字符串(如字符数组),但Java一般使用String类作为字符串的标准格式,Java编译器把字符串值作为String对象;

String对象一旦创建就不能改变。如果需要进行大量的字符串修改操作,应该使用StringBuilder/StringBuffer类或者字符串数组,最终结果可以被转换成String对象;

StringBuffer:线程安全的可变字符序列;

一个类似于String的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容;

每个字符串缓冲区都有一定的容量。只要字符串缓冲区所包含的字符序列的长度没有超出此容量,就无需分配新的内部缓冲区数组;

如果内存缓冲区溢出,则此容量自动增大;

StringBuilder:从JDK1.5开始,为StringBuffer类补充了一个单个线程使用的等价类,即StringBuffer;

与StringBuffer相比,通常应该优先使用StringBuilder类,因为它支持所有相同的操作,但由于它不执行同步,所有速度更快;

常用方法:

public int length()

public int capacity()

public StringBuffer append(String str)

public StringBuffer insert(int offset,String str)

public int indexOf(String str)

public int indexOf(String str,int fromIndex)

public int lastIndexOf(String str)

public StringBuffer reverse()

public String toString()

 public class StringBuilderDemo{
public static void main(String []args){
//StringBuilder sb="abc";//无此声明方式
//StringBuilder sb=new StringBuilder();//默认16个字符大小的容量
//StringBuilder sb=new StringBuilder(100);//初始化容量大小的动态字符串
//StringBuilder sb=new StringBuilder("abc"); StringBuilder sb=new StringBuilder();
sb.append("hello");
sb.append(1);
sb.append(1.5);
sb.append(true);
System.out.println(sb.length());
System.out.println(sb.capacity()); sb.insert(5,"world");
System.out.println(sb.toString());
sb.replace(5,7,"el");
System.out.println(sb.toString());
System.out.println(sb.indexOf("el"));
System.out.println(sb.reverse());
}
}

自定义一个自己的StringBuilder的类

案例介绍:自定义一个MyStringBuilder类来实现StringBuilder的功能;

案例设计:实现append方法追加字符串的功能;实现length方法统计字符的个数;实现capacity()方法获取容器大小;实现toString()方法,完成字符串的输出;

 import java.util.Arrays;
public class MyStringBuilderDemo{
public static void main(String []args){
MyStringBuilder msb=new MyStringBuilder();
msb.append("hello").append("java").append("12345678");
System.out.println("字符个数:"+msb.length());
System.out.println("总容量大小:"+msb.capacity());
System.out.println("输出字符串:"+msb.toString());
}
} class MyStringBuilder{
private char[] value;//字符数组
private int count=0;//字符数组中存放字符的个数
public MyStringBuilder(){
value=new char[16];
}
public MyStringBuilder(int capacity){
value=new char[capacity];
}
public MyStringBuilder(String str){
value=new char[str.length()+16];
}
//得到字符数组中存放的字符个数
public int length(){
return count;
}
//返回容器的容量大小
public int capacity(){
return value.length;
}
//实现字符串的添加(追加)
public MyStringBuilder append(String str){
int len=str.length();//获取要添加的字符串的长度
//确保字符数组能放进去所添加的字符串
ensureCapacity(count+len);
//把要添加的字符串追加到新的指定数组的指定位置后
str.getChars(0,len,value,count);
count+=len;//元素的个数增加了len
return this;
} private void ensureCapacity(int capacity){
//数据超出容量大小
if(capacity-value.length>0){
int newCapacity=value.length*2+2;//扩容后的新字符数组大小
value=Arrays.copyOf(value,newCapacity);
}
}
//把字符数组的字符转换成字符串格式
public String toString(){
return new String(value,0,count);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893