Android/Java 中的 String, StringBuffer, StringBuilder的区别和使用
Android 中的 String, StringBuffer 和 StringBuilder 是移动手机开发中经常使用到的字符串类。做为基础知识是必须要理解的,这里做一些总结。
A、区别
可以从以下两个方面来理解
1、不变和可变。
String类:使用数组保存字符串,类中的定义如下:
private final char value[];
注意到,因为用了final修饰符,所以可以知道string对象是长度不可变的。
StringBuilder和StringBuffer类:都是继承自AbstractStringBuilder类,在AbstractStringBuilder中也是使用动态的字符数组保存字符串,定义如下:
char[] value
注意到,未用到final修饰符,故可知这两种对象都是可变的。
2、是否线程安全。
String的对象是不可变的,因此它是线程安全的。
StringBuffer对方法加了同步锁或者对调用的方法加了同步锁,所以线程是安全的。如下代码所示:
public synchronized StringBuffer reverse() {
super.reverse();
return this;
}
StringBuilder对方法没有进行同步锁,所以是非线程安全的。
注意:AbstractStringBuilder是StringBuilder和StringBuffer类的公共父类,它定义了一些字符串的基本操作的共同方法。
B、共同点
1、在多线程环境下,StringBuffer的效率要比StringBuilder低。
2、抽象类AbstractStringBuilder是StringBuilder和StringBuffer类的公共父类。他们都调用父类的公共方法。如supper.append(...);
C、属性方法和使用
String的常用公共方法:
| public int lenght() | 返回字符串的长度。 |
| public char charAt(int index) | 返回字符串位置index处的字符。 |
| public boolean equals(Object o) | 比较两个字符串对象,相等则返回true;反之返回false。 |
| public int compareTo(String s) | 比较两个字符串字典顺序,相等返回0,s大于当前字符串返回一个负值,s小于当前串返回一个正值。 |
| public boolean regionMatches(int toffset,String other,int ooffset,int len) | 从当前字符串位置toffset开始寻找字符串other中起始位置为ooffset 长度为len 的子串。如发现匹配,返回true; 否则,返回false。 |
| public boolean startsWith(String prefix) | 从当前字符串的起始位置开始寻找字符串 prefix。如发现匹配,返回true;否则,返回false。 |
| public boolean endsWith(String suffix) | 如当前字符串的结尾子串与 suffix 匹配,返回true;否则,返回false。 |
| public int indexOf(String str) | 在当前字符串中寻找与str匹配的子串,返回首次匹配的起始下表值;无匹配返回-1。 |
| public String substring(int beginIndex,int endIndex) | 在当前字符串中,求从起始位置 beginIndex 到结束位置 endIndex 的子串。 |
| public String concat(String str) | 将当前字符串与str连接,返回连接后的字符串。 |
| public String toLowerCase() | 将当前字符串全转换为小写形式。 |
| public String toUpperCase() | 将当前字符串转换为大写形式。 |
| public char toCharArray() | 将当前字符串转换为字符数组。 |
| public Static String valueOf(type variable) | 把variable 转换为字符串,其中 type 表示 variable 的数据类型。 |
字符串连接的方法:
String a = "abc"
String b = "xkk"
b +=a;
StringBuffer的常用公共方法
| public int length() | 返回缓冲区的字符数 |
| public int capacity() | 返回缓冲区的容量大小,其值为:字符串长度+16。 |
| public synchronized StringBuffer append(type variable) | 把variable转换为字符串,然后与当前字符串连接。 |
| public synchronized StringBuffer append(Char(char ch) | 把字符ch连接到当前串尾。 |
| public synchronized StringBuffer insert(int offset,type variable) | 把variable转换为字符串,然后插入到当前串中由offset指定的位置。 |
| public synchronized StringBuffer insert(int offset,char ch) | 把字符 ch 插入到当前串由ofset指定的位置。 |
| public synchronized String toString() | 把StringBuffer转换为字符串String。 |
字符串连接方法
StringBuffer str= new StringBuffer("kkkxxx.");
str.append("xkk.");
StringBuilder的常用公共方法
| StringBuilder | append(boolean b) |
| Appends the string representation of the boolean argument to the sequence. | |
| StringBuilder | append(char c) |
| Appends the string representation of the char argument to this sequence. | |
| StringBuilder | append(char[] str) |
| Appends the string representation of the char array argument to this sequence. | |
| StringBuilder | append(char[] str, int offset, int len) |
| Appends the string representation of a subarray of the char array argument to this sequence. | |
| StringBuilder | append(CharSequence s) |
| 向此 Appendable 添加指定的字符序列。 | |
| StringBuilder | append(CharSequence s, int start, int end) |
| Appends a subsequence of the specified CharSequence to this sequence. | |
| StringBuilder | append(double d) |
| Appends the string representation of the double argument to this sequence. | |
| StringBuilder | append(float f) |
| Appends the string representation of the float argument to this sequence. | |
| StringBuilder | append(int i) |
| Appends the string representation of the int argument to this sequence. | |
| StringBuilder | append(long lng) |
| Appends the string representation of the long argument to this sequence. | |
| StringBuilder | append(Object obj) |
| Appends the string representation of the Object argument. | |
| StringBuilder | append(String str) |
| Appends the specified string to this character sequence. | |
| StringBuilder | append(StringBuffer sb) |
| 将指定的 StringBuffer 添加到此序列。 | |
| StringBuilder | appendCodePoint(int codePoint) |
| Appends the string representation of the codePoint argument to this sequence. | |
| int | capacity() |
| Returns the current capacity. | |
| char | charAt(int index) |
| Returns the char value in this sequence at the specified index. | |
| int | codePointAt(int index) |
| Returns the character (Unicode code point) at the specified index. | |
| int | codePointBefore(int index) |
| Returns the character (Unicode code point) before the specified index. | |
| int | codePointCount(int beginIndex, int endIndex) |
| Returns the number of Unicode code points in the specified text range of this sequence. | |
| StringBuilder | delete(int start, int end) |
| Removes the characters in a substring of this sequence. | |
| StringBuilder | deleteCharAt(int index) |
| Removes the char at the specified position in this sequence. | |
| void | ensureCapacity(int minimumCapacity) |
| Ensures that the capacity is at least equal to the specified minimum. | |
| void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) |
| Characters are copied from this sequence into the destination character array dst. | |
| int | indexOf(String str) |
| Returns the index within this string of the first occurrence of the specified substring. | |
| int | indexOf(String str, int fromIndex) |
| Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. | |
| StringBuilder | insert(int offset, boolean b) |
| Inserts the string representation of the boolean argument into this sequence. | |
| StringBuilder | insert(int offset, char c) |
| Inserts the string representation of the char argument into this sequence. | |
| StringBuilder | insert(int offset, char[] str) |
| Inserts the string representation of the char array argument into this sequence. | |
| StringBuilder | insert(int index, char[] str, int offset, int len) |
| Inserts the string representation of a subarray of the str array argument into this sequence. | |
| StringBuilder | insert(int dstOffset, CharSequence s) |
| Inserts the specified CharSequence into this sequence. | |
| StringBuilder | insert(int dstOffset, CharSequence s, int start, int end) |
| Inserts a subsequence of the specified CharSequence into this sequence. | |
| StringBuilder | insert(int offset, double d) |
| Inserts the string representation of the double argument into this sequence. | |
| StringBuilder | insert(int offset, float f) |
| Inserts the string representation of the float argument into this sequence. | |
| StringBuilder | insert(int offset, int i) |
| Inserts the string representation of the second int argument into this sequence. | |
| StringBuilder | insert(int offset, long l) |
| Inserts the string representation of the long argument into this sequence. | |
| StringBuilder | insert(int offset, Object obj) |
| Inserts the string representation of the Object argument into this character sequence. | |
| StringBuilder | insert(int offset, String str) |
| Inserts the string into this character sequence. | |
| int | lastIndexOf(String str) |
| Returns the index within this string of the rightmost occurrence of the specified substring. | |
| int | lastIndexOf(String str, int fromIndex) |
| Returns the index within this string of the last occurrence of the specified substring. | |
| int | length() |
| Returns the length (character count). | |
| int | offsetByCodePoints(int index, int codePointOffset) |
| Returns the index within this sequence that is offset from the given index by codePointOffset code points. | |
| StringBuilder | replace(int start, int end, String str) |
| Replaces the characters in a substring of this sequence with characters in the specified String. | |
| StringBuilder | reverse() |
| Causes this character sequence to be replaced by the reverse of the sequence. | |
| void | setCharAt(int index, char ch) |
| The character at the specified index is set to ch. | |
| void | setLength(int newLength) |
| Sets the length of the character sequence. | |
| CharSequence | subSequence(int start, int end) |
| Returns a new character sequence that is a subsequence of this sequence. | |
| String | substring(int start) |
| Returns a new String that contains a subsequence of characters currently contained in this character sequence. | |
| String | substring(int start, int end) |
| Returns a new String that contains a subsequence of characters currently contained in this sequence. | |
| String | toString() |
| Returns a string representing the data in this sequence. | |
| void | trimToSize() |
| Attempts to reduce storage used for the character sequence. |
字符串连接方法
StringBuilder sb2 = new StringBuilder();
sb2.append( "xxxkkk" );
D、参考资料
1、http://www.cnblogs.com/xudong-bupt/p/3961159.html(java中String、StringBuffer、StringBuilder的区别)
2、http://www.apihome.cn/api/java/StringBuilder.html(StringBuilder(示例,出错代码))
3、http://blog.csdn.net/zhzhl29290029/article/details/12339981(黑马程序--JAVA字符串String、StringBuffer、StringBuilder、基本数据类型包装)
Android/Java 中的 String, StringBuffer, StringBuilder的区别和使用的更多相关文章
- 在JAVA中,String,Stringbuffer,StringBuilder 的区别
首先是,String,StringBuffer的区别 两者的主要却别有两方面,第一是线程安全方面,第二是效率方面 线程安全方面: String 不是线程安全的,这意味着在不同线程共享一个String ...
- 重温java中的String,StringBuffer,StringBuilder类
不论什么一个系统在开发的过程中, 相信都不会缺少对字符串的处理. 在 java 语言中, 用来处理字符串的的类经常使用的有 3 个: String.StringBuffer.StringBuilder ...
- java中的String,StringBuffrer,Stringbuilder的区别
简单描述下 效率:StringBuilder>StringBuffer>String 使用场景: 如果要操作少量的数据用 = String 单线程操作字符串缓冲区 下操作大量数据 = St ...
- [置顶] String StringBuffer StringBuilder的区别剖析
这是一道很常见的面试题目,至少我遇到过String/StringBuffer/StringBuilder的区别:String是不可变的对象(final)类型,每一次对String对象的更改均是生成一个 ...
- String,StringBuffer,StringBuilder的区别及其源码分析
String,StringBuffer,StringBuilder的区别这个问题几乎是面试必问的题,这里做了一些总结: 1.先来分析一下这三个类之间的关系 乍一看它们都是用于处理字符串的java类,而 ...
- String,StringBuffer,StringBuilder的区别
public static void main(String[] args) { String str = new String("hello...."); StringBuffe ...
- Question 20171115 String&&StringBuffer&&StringBuilder的区别与联系?
Question 20171114 String&&StringBuffer&&StringBuilder的区别和联系 创建成功的String对象,其长度是固定的,内容 ...
- Java中的String,StringBuilder,StringBuffer三者的区别
最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下, ...
- Java中的String,StringBuilder,StringBuffer三者的区别(转载)
最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下, ...
随机推荐
- 使用qt写的简单的图片浏览器
功能特别简单,支持png,jpg,bmp,gif文件,支持自适应窗口大小,支持放大缩小,旋转功能还有点问题,支持上下按键选择图片 因为初学qt,所以很多东西都不太会,而且c++学的不是太好,没有怎么使 ...
- nginx之日志设置详解
nginx的日志设置 access_log access_log是服务器记录了哪些用户,哪些页面以及用户浏览器.ip和其他的访问信息:是一种非常详细的记录信息:如果我们不关心谁访问了我们,可以关闭: ...
- 【Git/GitHub学习笔记】一键更新多个git仓库至远程
因为同时在本地维护几个Github的仓库,每次更新后每个仓库要重复三步提交同步,有点麻烦. 发现可以写.sh文件来实现一键更新. 比如我要更新我的BlogBackup和CodeRepo两个仓库的代码如 ...
- Linux软件管理器(如何使用软件管理器来管理软件)
我们的Linux系统大部分都是某个Linux厂商的系统,所以这些厂商可以编译好一些软件来提供用户下载,用户下载完了之后就可以直接安装,从而省去了编译源码及其过程中的各种问题.这时我们就可以使用相应的软 ...
- EasyUi–8.datebox赋值的问题
这个问题要从EasyUI的datebox组件说起,小菜用这个组件的时候,发现用$("#id").val()这种形式,居然拿不到文本框的值! 经过度娘的帮助,发现可以用$(" ...
- Characters_of_the_Three_Kingdoms - 三国人物结构化数据
Characters_of_the_Three_Kingdoms - 三国人物结构化数据 三国人物结构化数据 为什么会有这个项目 需求1:摆脱网上那些长篇累牍的文章: 需求2:只是想简单查看下人物姓甚 ...
- 【DEV C++】 Error: ld returned 1 exit status
一般出现“ld returned 1 exit status”错误都是由于函数名称拼写错误造成的,或者在一个工程中不同的函数使用了同一个函数名,暂时还未遇到其他情况.
- 【企业通讯录app番外篇】怎么样创建服务端?
本文将指导大家如何去创建企业通讯录app的服务端 除了获取图片,客户端与服务端唯一的交互就是获取最新数据,如果该用户不能获取数据(用户名密码错误.用户离职被删)则返回空. 1:典型的交互流程如下: 用 ...
- ref:ThinkPHP Builder.php SQL注入漏洞(<= 3.2.3)
ThinkPHP Builder.php SQL注入漏洞(<= 3.2.3) ref:https://www.jianshu.com/p/18d06277161e TimeSHU 2018.04 ...
- CodeForces 785B Anton and Classes
简单判断. 找第一类区间中$R$最大的,以及第二类区间中$L$最小的,判断距离. 找第二类区间中$R$最大的,以及第一类区间中$L$最小的,判断距离. 两种情况取个最大值即可. #include &l ...