java tools -String class StringBuffer StringBuilder

String class ----> reference type java.lang package

Common String pen questions
  == difference equals method
  Base types can be compared comparison reference types
    more basic types of reference type comparison value comparison address
  equals the comparison reference only type (method)
    default this comparison address
obj
    If you want to modify the comparison rules can override the equals method
    will be accompanied by written focuses hashCode method Normally override equals method
    such as such as Integer String class
  immutable characteristics * String of
    length and content
  String and differences StringBuffer
  StringBuffer and differences between the StringBuilder
  String objects storage
     "abc" ----> string constant pool
     new string ( "abc") - > heap memory
     "a" + "B" + "C" + "D"
  * string methods commonly used

1. Where there is java.lang package

No inheritance implement three interfaces Serializable, CharSequence, Comparable

2. How to build an object

String str = "abc"; // string constant values directly str (string constant pool)
   String STR = new new String (); // Object no argument constructor creates an empty
   String str = new String ( "abc " ); // constructor method to create the object with the string parameter
   String str = new String (byte [ ]) // each array element into the corresponding combined into char String
   String String new new STR = (char []) / / char each of the splice element in the array to a final String

3. String immutable characteristic

String class includes a private final char [] value;
   in two places length and content
   length -> final modified array length array itself modified constant final address array does not change
   the content -> private property can not be modified in the class external access

4. * common method

1. equals()

The equals Boolean = 1. (Object obj);
    // Methods inherited from class Object rewrite rule it changes the comparison literal string
    == equals () distinction
    equalsIgnoreCase ();
    Here Insert Picture Description

2. hashCode()

the hashCode = int ();
// inherited from Object overwriting it current each char string element disassembled sum multiplied by 31

3. compareTo()

int = compareTo(String str);
//实现自Comparable接口 实现啦 按照字典(Unicode编码)索引的顺序比较

4. toString()

String = toString();
没有重写过的toString从Object类中继承过来的
类名@hashCode(16进制形式)
String类重写啦 返回的是String对象的字面值

5. charAt()

char = charAt(int index);//“abc” 0–>a
//返回给定index对应位置的那个char值
int = codePointAt(int index);//“abc” 0–>97
//返回给定index对应位置的那个char所对应的code码

6. length()

int = length();
//返回字符串的长度

7. concat()

String = concat(String);
将给定的字符串拼接在当前字符串之后
注意: 方法执行完毕需要接受返回值 String的不可变特性
concat方法与 +拼接的性能问题
开发中若遇到频繁的拼接字符串—>通常使用StringBuilder/StringBuffer

8. contains()

boolean = contains(CharSequence s);
判断给定的s是否在字符串中存在

9. startsWith()/endsWith() 开头/结尾

startsWith(String prefix);
endsWith(String suffix);
判断此字符串是否已xx开头/结尾

10. getBytes()

byte[] = getBytes(); —> getBytes(String charsetName);
char[] = toCharArray();
将当前的字符串转化成数组 “我爱你中国” char[] ‘我’ ‘爱’ ‘你’ ‘中’ ‘国’

11. indexOf()

int index = indexOf(int/String str [,int fromIndex] );  四个方法重载
找寻给定的元素在字符串中第一次出现的索引位置  若字符串不存在则返回-1
lastIndexOf(int/String str , [int fromIndex]);
找寻给定的元素在字符串中最后一次出现的索引位置 若不存在则返回-1

12. isEmpty()

boolean = isEmpty();
判断当前字符串是否为空字符串 (length是否为0)
注意: 与null之间的区别

13. replace() 替换

replaceAll(String regex,String replacement); //正则表达式
replaceFirst(); 换第一次出现的那个字串
将给定的字符串替换成另外的字符串

14. split()

String[] = split(String regex [,int limit限度界限]);
按照给定的表达式将原来的字符串拆分开的

15. substring() 截取

String = substring(int beginIndex [,int endIndex]);
将当前的字符串截取一部分
从beginIndex开始至endIndex结束 [beginIndex,endIndex)
若endIndex不写 则默认到字符串最后

16. toUpperCase()/toLowerCase() 大写/小写

String = toUpperCase();
String = toLowerCase();
将全部字符串转换成大写/小写

17. trim()

String = trim();
去掉字符串前后多余的空格

18. matches()

boolean = matches(String regex)
regular有规律的 expression表达式
正则表达式

StringBuffer和StringBuilder

StringBuffer 是线程安全的
StringBuilder是线程不安全的

1. 所属的包 java.lang包

2. 继承AbstractStringBuilder 间接继承 Object

实现接口Serializable,CharSequence,Appendable
  StringBuffer/StringBuilder没有compareTo方法
  StringBuffer/StringBuilder含有一个String没有的方法 append();拼接

3. 特性

可变字符串 char[] value; 动态扩容

4. 对象的构建

//无参数构造方法 构建一个默认长度16个空间的对象 char[]
  StringBuilder builder = new StringBuilder();
  //利用给定的参数 构建一个自定义长度空间的对象 char[]
  StringBuilder builder = new StringBuilder(20);
  //利用带String参数的构造方法 默认数组长度字符串长度+16个
  StringBuilder builder = new StringBuilder(“abc”)

5. StringBuilder中常用的方法

append()

最主要的方法 append()  频繁的拼接字符串的时候使用此方法 提高性

ensureCapacity(int minimumCapacity)

确保底层数组容量够用

capacity();

字符串底层char[]的容量

length();

字符串有效元素个数(长度)

setLength();

设置字符串的有效元素个数

char = charAt(int index);

int = codePointAt(int index);

String = substring(int start [,int end]);

注意需要接受返回值 看见截取出来的新字符串效果

StringBuilder = delete(int start [,int end]);

StringBuilder类中独有的方法String类没有
将start到end之间的字符串删掉 不用接受返回值就看到效果啦

StringBuilder = deleteCharAt(int index);

String类中没有的方法
将给定index位置的某一个字符删除掉啦

int = indexOf(String str [,int fromIndex]);

int = lastIndexOf(String str [,int fromIndex])

找寻给定的str在字符串中第一次出现的索引位置 带重载 则从某一个位置开始找

将给定的value插入在index位置之上

replace(int start,int end,String str);

将start和end之间的部分替换成str
builder.replace(2,5,“zzt”);

setCharAt(int index,char value);

将index位置的字符改成给定的value

toString()

将StringBuilder对象 构建成一个string对象 返回

trimToSize()

将数组中无用的容量去掉 变成length长度的数组

知识总结

1.StringBuilder类不一定需要

是为了避免String频繁拼接修改字符串信息的时候才用的
底层数组是可变的 提高了性能

2.常用方法

与String类不同的独有方法
 append()  insert() delete() deleteCharAt() reverse()
与String类相同的方法
 length() charAt() codePointAt() indexOf() lastIndexOf()
 substring() replace()名字相同 用法不一致
 不是很常用的方法
  ensureCapacity() capacity() setLength() trimToSize(); setCharAt();

3.String家族笔试中经常容易考察的知识点

1.String所属的包 继承关系 实现接口

java.lang 继承Object 接口Serializable,CharSequence,Comparable

2.String构建方式

常量 构造方法

3.String对象内存结构

字符串常量区 new堆内存对象
== equals()区别
“a”+“b”+“c”

4.String不可变特性

长度及内容

5.String中的常用方法—与StringBuilder的区别

concat();  toUpperCase();

6.String和StringBuilder区别 | String和StringBuffer区别

String String immutable
  JDK1.0
  has an interface Comparable
  non-variable length and content reflected in
  some methods the compareTo the toUpperCase StringBuilder not the concat
 StringBuilder variable strings
  JDK1.5
  has an interface Appendable
  variable string is not modified final layer may be an array expansion
  there are some ways String no append () insert () delete ( ) reverse ()

7.StringBuffer different and StringBuilder

StringBuffer earlier version 1.0
the StringBuilder later version 1.5
earlier versions of thread synchronization security is relatively high efficiency is relatively low
later versions of asynchronous thread safety is relatively low efficiency is relatively high

Published 30 original articles · won praise 0 · Views 6639

Guess you like

Origin blog.csdn.net/qq_37710756/article/details/103674086