Java- string -String

String

All string literals (eg Java programs "abc") can be seen as the realization of such an instance.

Features:

1, the string type String itself is declared final, means that we can not inherit String.

2, the target character string is immutable objects, meaning that once modified, the new object is generated

  We modified the string, if you want to get new content, must be re-accepted.

3, String internal object is saved with the array of characters

  After there is a char [] value array, JDK1.9 byte [] array before JDK1.9

. 4, this class String char [] values ​​are final modification of the array, means that the array can not be changed, then it is the private modifier, can not directly operate outside it, all the methods are used to provide a String object to represent a new repair

After the change of content, so ensure the immutable String object.

5, because String objects designed to be immutable, so it has a string constant pool to save a lot of constant objects can be shared

  If the modification involves a large number of strings, it is recommended to use StringBuffer or StringBuilder

Creating String objects

1, literal constant value

  String str = "hello";

2, using the constructor

  • public String() : String object initialization newly created, so that it represents an empty character sequence.

  • String(String original): Initializes a newly created Stringobjects to represent the same parameters as a sequence of characters; in other words, the newly created string is a copy of the argument string.

  • public String(char[] value) : To construct a new String by the current character array argument.

  • public String(char[] value,int offset, int count) : To construct a new String by part of an array of characters.

  • public String(byte[] bytes) : To construct a new String by using the platform's default character set to decode the current byte array parameter.

  • public String(byte[] bytes,String charsetName) : Construction of new String by using the specified character set currently decoded byte array parameter.

3, static methods

  • static String copyValueOf (char [] data): Returns the array representing the sequence of characters String

  • static String copyValueOf (char [] data, int offset, int count): Returns the array representing the sequence of characters String

  • String static valueOf (char [] Data): Returns the array representing the sequence of characters String

  • static String valueOf (char [] data, int offset, int count): Returns the array representing the sequence of characters String

  • String static valueOf (XX value): XX supports a variety of data types, the parameter value returns the string representation of various data types.

4、xx.toString() 

  S = the StringBuffer new new the StringBuffer (XX);
  String = s.toString STR ();: return a String object

5, and the string stitching +

  And any data type "String" is spliced, the result is a string

The number of String objects

String str = "hello";  //1个

String str = new String("atguigu");  //2个

String s1 = "hello";//1个
String s2 = "world";//1个

String s3 = s1 + s2 + "java"; // "java" 1 th, s1 + s2 splicing a result, a final result

Memory analysis String object

Splicing String object

1、+

(1) + Constant Constant: The result is constant pool

(2) variable and constant or variable with the variables: result stack

  Point + constant string constants stack: the stack results in

(3) call intern after splicing method: The results in the constant pool

2, concat: the result is a mosaic of new string, even if it is two constants mosaic objects are in the heap

S1 = String "Hello";
String S2 = "World";

String S1 + S3 = "World"; // string content is HelloWorld S3, S1 is variable, "world" constants, variables, constants result in a heap +
String s4 = s1 + s2; // s4 string contents are helloworld, s1 and s2 are variables, outcome variables variables in the heap +

Comparison of String objects

1 ==: Compare Address

2, equals (xx): compare string contents, strictly case-sensitive. Because String type override equals

3, equalsIgnoreCase (xx): comparison string content, case-insensitive

4, compareTo (xx): Comparison of the size of the string, character encoding in accordance with the comparison value, strictly case sensitive. String type rewrite Comparable interface of abstract methods, natural ordering

5, (xx) compareToIgnoreCase: comparing the size of the string, character code values ​​according to the comparison case-insensitive

6, java.text.Collator: text verifier comparing the size, compare the size of a natural language in accordance with the order specified locale (dictionary ordering)

Relatively empty character

Null string

  String str1 = "";
  String str2 = new String();
  String str3 = new String("");

To determine whether a string is an empty string

  if("".equals(str))  //推荐

  if(str!=null && str.isEmpty())

  if(str!=null && str.equals(""))

  if(str!=null && str.length()==0)

String class of common API

No. The method of signature Methods Features
1 String() Creating an empty string
2 String(String original) Create a new string from the original
3 static String valueOf(xx value) Create a string value based on content
4 String intern() The contents of the string stored in the constant pool
5 String concat() String concatenation
6 boolean equals(Object obj) Determine the current string with the specified string content is already in strictly case-sensitive
7 boolean equalsIgnoreCase(String obj) Current string is determined whether the contents of a specified string is case insensitive,
8 int compareTo(String str) Compare the size of the current string with the specified string, strictly case-sensitive
9 int compareToIgnoreCase(String str) Compare the current size of the string with the specified string, case-insensitive
10 boolean isEmpty() Determining whether the current string is empty
11 int length() Returns the string length of the current
12 String toLowerCase() The current string lowercase
13 String toUpperCase() The current string to uppercase
14 String trim() White space before and after the current string is removed
15 boolean contains(xx) Determining whether the current string comprising xx
16 int indexOf(xx) Xx Find the first occurrence of the string in the current index
17 int lastIndexOf(xx) Find the last occurrence of the subscript xx in the current string
18 String substring(int beginIndex) 从当前字符串的[beginIndex, 最后]截取一个子串
19 String substring(int beginIndex, int endIndex) 从当前字符串的[beginIndex, endIndex)截取一个子串
20 char charAt(index) 返回当前字符串[index]位置字符
21 char[] toCharArray() 将当前字符串的内容用一个字符数组返回
22 String(char[] value) 用value字符数组的元素构建一个新字符串
23 String(char[] value,int offset, int count) 用value字符数组的[offset]开始的count个字符构建一个新字符串
24 static String copyValueOf(char[] data) 用data字符数组的元素构建一个新字符串
25 static String copyValueOf(char[] data, int offset, int count) 用data字符数组的[offset]开始的count个字符构建一个新字符串
26 static String valueOf(char[] data) 用data字符数组的元素构建一个新字符串
27 static String valueOf(char[] data, int offset, int count) 用data字符数组的[offset]开始的count个字符构建一个新字符串
28 byte[] getBytes() 将当前字符串按照平台默认字符编码方式编码为字节序列
29 byte[] getBytes(字符编码方式) 将当前字符串按照指定字符编码方式编码为字节序列
30 String(byte[] bytes) 将bytes字节序列按照平台默认字符编码方式解码为字符串
31 String(byte[] bytes,String charsetName) 将bytes字节序列按照指定字符编码方式解码为字符串
32 boolean startsWith(xx) 判断当前字符串是否以xx开头
33 boolean endsWith(xx) 判断当前字符串是否以xx结尾
34 boolean matchs(xx) 判断当前字符串是否满足xx正则
35 String replace(xx,yy) 将当前字符串中所有xx替换为yy
36 String replaceFirst(xx,value) 将当前字符串中第一个满足xx正则的字符替换为value
37 String repalceAll(xx, value) 将当前字符串中所有满足xx正则的字符替换为value
38 String[] split(xx) 将当前字符串按照xx正则拆分为多个字符串
39 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将当前字符串的[srtBegin,srcEnd)部分字符复制到dst字符数组中,dst数组从[dstBegin]开始存储

StringBuffer和StringBuilder

String类的对象是不可变字符序列,StringBuffer和StringBuilder的对象是可变字符序列。

StringBuffer:老点,线程安全的,因为它的方法有synchronized修饰

StringBuilder:JDK1.5之后引入的,线程不安全,单线程情况下推荐使用。

常用的API,StringBuilder、StringBuffer的API完全一致

StringBuilder、StringBuffer的API

序号 方法签名 方法区功能简介
1 StringBuffer() 创建一个空的可变字符序列,默认长度16
2 StringBuffer(String str) 用字符串str内容创建一个可变字符序列
3 StringBuffer append(数据类型 b) 在当前字符序列后面追加b
4 StringBufferinsert(int index, 数据类型 s) 在当前字符序列[index]插入s
5 StringBuffer delete(int start, int end) 删除当前字符序列[start,end)部分字符
6 StringBuffer deleteCharAt(int index) 删除当前字符序列[index]位置字符
7 void setLength(int newLength) 修改当前字符序列的长度为newLength
8 void setCharAt(int index, char ch) 替换当前字符序列[index]位置字符为ch
9 StringBuffer reverse() 将当前字符序列内容反转
10 StringBuffer replace(int start, int end, String str) 替换当前字符序列[start,end)部分字符为str
11 int indexOf(String str) 在当前字符序列中开始查找str第一次出现的下标
12 int indexOf(String str, int fromIndex) 在当前字符序列[fromIndex]开始查找str第一次出现的下标
13 int lastIndexOf(String str) 在当前字符序列中开始查找str最后一次出现的下标
14 int lastIndexOf(String str, int fromIndex) 在当前字符序列[fromIndex]开始查找str最后一次出现的下标
15 String substring(int start) 截取当前字符序列[start,最后]部分构成一个字符串
16 String substring(int start, int end) 截取当前字符序列[start,end)部分构成一个字符串
17 String toString() 将当前可变字符序列的内容用String字符串形式表示
18 void trimToSize() 如果缓冲区大于保存当前字符序列所需的存储空间,则将重新调整其大小,以便更好地利用存储空间。
19 int length() 返回当前字符序列的长度
20 char charAt(int index) 返回当前字符序列[index]位置字符
21 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将当前字符串的[srtBegin,srcEnd)部分字符复制到dst字符数组中,dst数组从[dstBegin]开始存储

 

 正则表达式

字符编码

ASCII码
  • 0~31及127(共33个)是控制字符或通信专用字符(其余为可显示字符),如控制符:LF(换行)、CR(回车)、FF(换页)、DEL(删除)、BS(退格)

  • 32~126(共95个)是字符(32是空格),其中48~57为0到9十个阿拉伯数字。

  • 65~90为26个大写英文字母,97~122号为26个小写英文字母,其余为一些标点符号、运算符号等。

Unicode

 为解决一个问题:如果一份文档中含有不同国家的不同语言的字符,那么无法在一份文档中显示所有字符。Unicode字符集涵盖了目前人类使用的所有字符,并为每个字符进行统一编号,分配唯一的字符码(Code Point)

 UTF-8

 它是一种变长的编码方式。它可以使用1~4个字节表示一个符号。从unicode到uft-8并不是直接的对应,而是要过一些算法和规则来转换(即Uncidoe字符集≠UTF-8编码方式

 Unicode只是定义了一个庞大的、全球通用的字符集,并为每个字符规定了唯一确定的编号,具体存储成什么样的字节流,取决于字符编码方案。推荐的Unicode编码是UTF-16

和UTF-8。

 

Guess you like

Origin www.cnblogs.com/Open-ing/p/11965514.html